154

I am using matplotlib to make some graphs and unfortunately I cannot export them without the white background.

sample plot with solid white background

In other words, when I export a plot like this and position it on top of another image, the white background hides what is behind it rather than allowing it to show through. How can I export plots with a transparent background instead?

Gordon Gustafson
  • 38,624
  • 25
  • 111
  • 155
Cupitor
  • 10,240
  • 19
  • 62
  • 90

2 Answers2

245

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

Warren Weckesser
  • 102,583
  • 19
  • 173
  • 194
  • 1
    Is it possible to make the background transparent without having to save the file? Say we are seeing the output in Jupyter Notebook using `plt.show()`. Is there any way to make that itself transparent? So that when I copy and paste image from the jupyter notebook output the background stays transparent? – Meet Oct 25 '21 at 09:40
  • @meet, I think you should start a new question, or ask that over in https://discourse.matplotlib.org/ – Warren Weckesser Oct 25 '21 at 13:01
4

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.

Community
  • 1
  • 1
Stephane Rolland
  • 37,098
  • 33
  • 115
  • 165
  • Thanks but this doesn't help. I did knew that png is capable of being transparent but the output of matplotlib is not. Plus your suggestion only covers the case when you want to remove rectangles. I want my plot to be saved and my background to be removed. – Cupitor Apr 07 '13 at 01:16
  • 2
    As far as I remember you set which is the transparent colour in a png. file. Set the white colour as the transparency colour and there you go. – Stephane Rolland Apr 07 '13 at 01:22
  • I suppose 255 is white and when I go with it I get my plot lost! – Cupitor Apr 07 '13 at 01:30
  • that is strange. your plots only ? the graph in black and the units in black and the blue curve also ? – Stephane Rolland Apr 07 '13 at 01:43
  • Not black. Transparent! Let me send you example: Here is the code I run: import Image import ImageDraw im = Image.open("~/figure_1.png") transparent_area = (50,80,300,400) mask=Image.new('L', im.size, color=255) draw=ImageDraw.Draw(mask) draw.rectangle(transparent_area, fill=0) im.putalpha(mask) im.save('~/new.png') And here is the result: http://i47.tinypic.com/zx6tmx.png – Cupitor Apr 07 '13 at 01:46
  • 1
    there's this question about turning all white pixel to transparency ( not using the mask like I linked to you) http://stackoverflow.com/questions/765736/using-pil-to-make-all-white-pixels-transparent – Stephane Rolland Apr 07 '13 at 01:50