Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save image with matplotlib.pyplot [duplicate]

This is a very simple question but I must misunderstand the use of pyplot and figure or something else. I'm plotting some images and would like to save them instead of just showing them and saving them by hand. So far I've tried:

import matplotlib.pyplot as plt
fig = plt.figure()
plt.plot(d,c1[0:100],'b--',d,c2[0:100],'r--',d,c3[0:100],'g--',figure = fig) 
plt.ylabel("concentration")
plt.xlabel("distance")
plt.show()
plt.savefig('./Results/evol_conc_v'+str(vinit)+'a_'+str(a)+'.png')

The file created is blank, but the image showed was good. The existing similar question don't seem to apply.

like image 476
alpagarou Avatar asked Apr 21 '15 14:04

alpagarou


Video Answer


1 Answers

Get rid of the

plt.show()

or put it below the savefig call.

Or you do this

plt.show()
fig.savefig('./Results/evol_conc_v'+str(vinit)+'a_'+str(a)+'.png') # Use fig. here

since you already create a figure object at the beginning.

like image 68
lmNt Avatar answered Oct 04 '22 18:10

lmNt