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)
Best Answer
First, what happens when
T0 is not None
? I would test that, then I would adjust the values I pass toplt.subplot()
; maybe try values 131, 132, and 133, or values that depend whether or notT0
exists.Second, after
plt.show()
is called, a new figure is created. To deal with this, you canCall
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:
In your code, 'tesssttyyy.png' is blank because it is saving the new figure, to which nothing has been plotted.