Question
Asked By – tylerthemiler
I am trying to save plots I make using matplotlib; however, the images are saving blank.
Here is my code:
plt.subplot(121)
plt.imshow(dataStack, cmap=mpl.cm.bone)
plt.subplot(122)
y = copy.deepcopy(tumorStack)
y = np.ma.masked_where(y == 0, y)
plt.imshow(dataStack, cmap=mpl.cm.bone)
plt.imshow(y, cmap=mpl.cm.jet_r, interpolation='nearest')
if T0 is not None:
plt.subplot(123)
plt.imshow(T0, cmap=mpl.cm.bone)
#plt.subplot(124)
#Autozoom
#else:
#plt.subplot(124)
#Autozoom
plt.show()
plt.draw()
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
Answer
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 plt.show()
is called, a new figure is created. To deal with this, you can
-
Call
plt.savefig('tessstttyyy.png', dpi=100)
before you callplt.show()
-
Save the figure before you
show()
by callingplt.gcf()
for “get current figure”, then you can callsavefig()
on thisFigure
object at any time.
For example:
fig1 = plt.gcf()
plt.show()
plt.draw()
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