How to export plots from matplotlib with transparent background?
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Peaceful Mind
--
Chapters
00:00 Question
00:41 Accepted answer (Score 262)
01:39 Answer 2 (Score 5)
02:19 Answer 3 (Score 1)
02:44 Thank you
--
Full question
https://stackoverflow.com/questions/1585...
Answer 1 links:
[Save plot to image file instead of displaying it using Matplotlib]: https://stackoverflow.com/questions/9622...
[Using PIL to make all white pixels transparent?]: https://stackoverflow.com/questions/7657...
[Python PIL: how to make area transparent in PNG?]: https://stackoverflow.com/questions/4379...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #matplotlib #plot #transparency
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Peaceful Mind
--
Chapters
00:00 Question
00:41 Accepted answer (Score 262)
01:39 Answer 2 (Score 5)
02:19 Answer 3 (Score 1)
02:44 Thank you
--
Full question
https://stackoverflow.com/questions/1585...
Answer 1 links:
[Save plot to image file instead of displaying it using Matplotlib]: https://stackoverflow.com/questions/9622...
[Using PIL to make all white pixels transparent?]: https://stackoverflow.com/questions/7657...
[Python PIL: how to make area transparent in PNG?]: https://stackoverflow.com/questions/4379...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #matplotlib #plot #transparency
#avk47
ACCEPTED ANSWER
Score 288
Use the matplotlib savefig function with the keyword argument transparent=True to save the image as a png file.
In [28]: import numpy as np
In [29]: from matplotlib.pyplot import plot, savefig
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:

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.

ANSWER 2
Score 5
As a reminder, the plt.savefig() should be written before the plt.show(), otherwise, a transparent image will be created (without the actual plot).
For high-quality images:
plt.savefig('filename.png', format='png', dpi='600', transparent=True)
plt.show()