How to export plots from matplotlib with transparent background? How to export plots from matplotlib with transparent background? python python

How to export plots from matplotlib with transparent background?


Use the matplotlib savefig function with the keyword argument transparent=True to save the image as a png file.

In [30]: x = np.linspace(0,6,31)In [31]: y = np.exp(-0.5*x) * np.sin(x)In [32]: plot(x, y, 'bo-')Out[32]: [<matplotlib.lines.Line2D at 0x3f29750>]            In [33]: savefig('demo.png', transparent=True)

Result:demo.png

Of course, that plot doesn't demonstrate the transparency. Here's a screenshot of the PNG file displayed using the ImageMagick display command. The checkerboard pattern is the background that is visible through the transparent parts of the PNG file.

display screenshot


Png files can handle transparency.So you could use this question Save plot to image file instead of displaying it using Matplotlib so as to save you graph as a png file.

And if you want to turn all white pixel transparent, there's this other question : Using PIL to make all white pixels transparent?

If you want to turn an entire area to transparent, then there's this question: And then use the PIL library like in this question Python PIL: how to make area transparent in PNG? so as to make your graph transparent.