1

I want to save a plot with a transparent background using savefig. The code looks like this:

plt.savefig("filename.png", transparent=True)
plt.show()

It works well and saves the images as expected (some graphs on a transparent background). However, if I change the size of the plot, the output image becomes an empty image, that is, a completely transparent image with no graphs. This is how I changed the size:

plt.figure(figsize=(6.4, 4.8), dpi=100)
plt.savefig("filename.png", transparent=True)
plt.show()

I want a 640x480 image. Even if I don't change the dpi the output images are completely blank. How can i avoid this?

sentence
  • 8,213
  • 4
  • 31
  • 40
Asier R.
  • 443
  • 1
  • 5
  • 11
  • 1
    What happens moving `plt.figure(figsize=(6.4, 4.8), dpi=100)` at the beginning of the plotting code? – sentence Jun 17 '19 at 11:43
  • https://stackoverflow.com/a/4306340/11345381 – rtoijala Jun 17 '19 at 11:44
  • `plt.figure()` creates a figure. Directly after you create it you save it, so it has no content. See the duplicate on how to set the figure size. – ImportanceOfBeingErnest Jun 17 '19 at 11:45
  • 1
    @sentence that solved my problem, thank you. Also the people who marking this as duplicate, the links you provide don't answer my question. – Asier R. Jun 17 '19 at 11:48
  • @AsierR. : This is the relevant duplicate: [figsize-in-matplotlib-is-not-changing-the-figure-size](https://stackoverflow.com/questions/52274643/figsize-in-matplotlib-is-not-changing-the-figure-size) – Sheldore Jun 17 '19 at 12:18

0 Answers0