Fix Python – Matplotlib (pyplot) savefig outputs blank image


Asked By – tylerthemiler

I am trying to save plots I make using matplotlib; however, the images are saving blank.

Here is my code:


y = copy.deepcopy(tumorStack)
y = == 0, y)

plt.imshow(y,, interpolation='nearest')

if T0 is not None:


plt.savefig('tessstttyyy.png', dpi=100)

And tessstttyyy.png is blank (also tried with .jpg)

Now we will see solution for issue: Matplotlib (pyplot) savefig outputs blank image


First, what happens when T0 is not None? I would test that, then I would adjust the values I pass to plt.subplot(); maybe try values 131, 132, and 133, or values that depend whether or not T0 exists.

Second, after is called, a new figure is created. To deal with this, you can

  1. Call plt.savefig('tessstttyyy.png', dpi=100) before you call

  2. Save the figure before you show() by calling plt.gcf() for “get current figure”, then you can call savefig() on this Figure object at any time.

For example:

fig1 = plt.gcf()
fig1.savefig('tessstttyyy.png', dpi=100)

In your code, ‘tesssttyyy.png’ is blank because it is saving the new figure, to which nothing has been plotted.

This question is answered By – Yann

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0