Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Matplotlib Transparent Plot Background: How-To

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Making a Matplotlib plot background transparent lets you place charts directly onto colored slides, websites, or documents without a white box clashing with the design. It also helps when you want to overlay multiple plots or save figures with a transparent layer for further editing in image software. You only need a working Python environment with Matplotlib installed, and a basic script that creates a figure and axes.

What You'll Need

  • Python 3.6 or newer
  • Matplotlib installed (pip install matplotlib)
  • A code editor or Jupyter notebook
  • An existing plot or a simple script to test with

Step-by-Step

  1. Create a basic figure and axes. Start with a minimal plot so you can see the effect clearly. For example:
    import matplotlib.pyplot as plt
    fig, ax = plt.subplots()
    ax.plot([1, 2, 3], [4, 5, 6])
    plt.show()
    This gives you a standard white-background figure inside a white window.
  2. Set the figure background to transparent. The figure is the outer canvas that holds everything. Use the facecolor parameter when creating it, or set it afterward:
    fig, ax = plt.subplots(facecolor='none')
    The value 'none' (or 'None') means fully transparent. You can also use fig.patch.set_alpha(0) after creation.
  3. Set the axes background to transparent. The axes is the area inside the plot where the data and grid lines appear. By default it has its own white background that covers the figure's transparency:
    ax.set_facecolor('none')
    Or equivalently: ax.patch.set_alpha(0). Without this step, you'll only see transparency around the plot box, not inside it.
  4. Check both patches. If you want the entire plot area (including where the tick labels and title sit) to be transparent, you need both fig.patch and ax.patch set to transparent. The figure patch is the outermost rectangle; the axes patch is the rectangle behind the plotted data.
  5. Save the figure with transparency enabled. The savefig function has a transparent parameter that forces all backgrounds to be transparent, regardless of the facecolor settings. This is the most reliable method:
    plt.savefig('plot.png', transparent=True)
    This works for PNG, PDF, and SVG formats. For PNG, the transparent areas will show a checkerboard pattern in some image viewers, which is normal.
  6. Verify the result. Open the saved file in an image editor or a viewer that supports transparency (like a web browser). If you see a white or checkered background, the transparency worked. If you see solid white, go to the Troubleshooting section below.
  7. For inline display in Jupyter, adjust the display settings. When using %matplotlib inline, the notebook may force a white background. Use %matplotlib notebook or save the figure and display the image file instead. You can also try:
    from IPython.display import display, Image
    display(Image(filename='plot.png'))
    This shows the saved transparent PNG directly.
  8. For interactive windows, transparency may not show. The default Matplotlib window (using TkAgg, QtAgg, etc.) typically renders a gray or white background regardless of your settings. Transparency is mainly visible in saved files or when embedding in a GUI that supports it. Don't be alarmed if the on-screen plot still looks opaque.

Common Mistakes

  • Only setting the figure background. Many users set fig.patch.set_alpha(0) but forget the axes patch. The axes background is a separate rectangle that covers the figure background inside the plot area.
  • Using alpha=0 on the wrong object. The alpha parameter on a line or marker controls the transparency of that element, not the background. You need facecolor='none' on the patches.
  • Forgetting transparent=True in savefig. Even if you set facecolors to 'none', some backends may still write a white background unless you pass this parameter.
  • Saving as JPG. JPEG format does not support transparency. Always use PNG, PDF, or SVG if you need transparent backgrounds.
  • Setting facecolor='white' somewhere else. If you use a style sheet or a theme that sets a white facecolor, your explicit 'none' may be overridden. Check the order of your commands.

Tips

  • Use transparent=True in savefig as a one-stop solution. It overrides all background settings and is the most foolproof method.
  • Combine with bbox_inches='tight' to remove extra whitespace around the plot: plt.savefig('plot.png', transparent=True, bbox_inches='tight').
  • For PDF output, transparency is preserved and can be edited in vector graphics programs like Inkscape or Adobe Illustrator.
  • Test with a colored background behind your plot to confirm transparency. For example, open an HTML file with a dark background and place the PNG there.
  • If you use plt.subplots() with multiple axes, you need to set transparency for each axes object individually (e.g., in a loop).
  • Remember that grid lines, ticks, and labels are separate from the background. Making the background transparent does not affect their visibility.

Troubleshooting

The saved image still has a white background. First, ensure you passed transparent=True to savefig. If you did, check the file format – if it's JPG, switch to PNG. Also confirm you didn't call plt.figure(facecolor='white') after setting it to 'none'. Finally, check if you're using a style that sets a white background; run plt.style.use('default') before your code.

The plot window shows a gray/white background even though I set transparency. This is expected. Interactive Matplotlib backends do not support transparent windows on most operating systems. The transparency only appears in saved files or when embedding the figure in a GUI toolkit that supports it (like PyQt with a transparent widget).

My Jupyter notebook shows a white box around the plot. The inline backend forces a white figure background. Save the figure to a file and display that file, or use %matplotlib notebook which allows interactive transparency in some environments.

I see a checkerboard pattern in my image viewer. That's not a bug – the checkerboard is how many viewers indicate transparency. Open the file in a browser or place it on a colored background to see the actual effect.

The transparency works for the figure but not inside the plot area. You forgot to set the axes background. Add ax.set_facecolor('none') or use plt.subplots(facecolor='none') and then ax.patch.set_alpha(0).

After saving, the text or lines look pixelated. This is unrelated to transparency. Increase the DPI: plt.savefig('plot.png', transparent=True, dpi=300).

Frequently Asked Questions

Q: Can I make only the axes background transparent but keep the figure background white?
A: Yes. Set ax.set_facecolor('none') but leave the figure facecolor as default (white). This is useful when you want the plot area to blend with a page but keep a white border around it.

Q: Does transparency work with all Matplotlib backends?
A: No. It works reliably when saving to file formats that support alpha (PNG, PDF, SVG). Interactive backends (TkAgg, QtAgg) usually ignore transparency for the window background.

Q: What's the difference between facecolor='none' and alpha=0?
A: facecolor='none' tells Matplotlib not to draw a background at all. alpha=0 makes the background fully transparent. The visual result is the same for saved files, but facecolor='none' is more explicit and less likely to cause issues with certain backends.

Q: Can I make the legend or text boxes transparent too?
A: Yes. For a legend, use legend(framealpha=0) or legend(facecolor='none'). For text boxes created with bbox, set bbox=dict(boxstyle='round', facecolor='none').

Q: Will transparency affect the file size?
A: Generally, PNG files with transparency are slightly larger than opaque ones because they store an alpha channel. PDF and SVG files are usually unaffected.

Q: How do I make the entire figure transparent including the title and tick labels?
A: The title and tick labels are text objects, not backgrounds, so they are already transparent by default (they have no background box unless you add one). Setting both figure and axes facecolors to 'none' will make the whole visible area transparent.