python - Matplotlib: Saved files in a loop aren't the same as in show() -
my program shows correct graph in plt.show()
pop not in fig.savefig one. i'm quite new python apologies if simple. i'm using python 2.7.10, windows (10).
import numpy np import matplotlib.pyplot plt data = np.genfromtxt('strike_details.txt') #, skip_header= 0 header= 3 information=10000 width = 5 files = 16 types = 4 length = information + header frames = data[header:length,0] fig= plt.figure() plt.grid(true) in range(0,int(files)): density=data[(header+i*length):(length+i*length),4] plt.plot(frames,density, label=data[i*length+1][2]) j in range (0,files/types): if i==(types*(j+1)-1): plt.legend(loc='best') plt.xlabel('$frames$', fontsize=22) plt.ylabel('$density$', fontsize=22) fig.savefig(str(data[j*length+1][0])+'_'+str(data[j*length+1][1])+'_'+str(data[j*length+1][2])+'.png',format='png', dpi=fig.dpi) plt.show() plt.clf()
the program produces 4 files different file names they're of first group see in plt.show pop up.
if missed out important let me know.
thanks,
lio
i think due mixing api-style , interactive-styles of matplotlib. when call plt.show()
link between active figure , fig
broken, , continue output first figure created. can reproduce problem minimal example:
import matplotlib.pyplot plt fig = plt.figure() n in range(0,10): plt.plot(list(range(0,n))) fig.savefig('test%d.png' % n) plt.show() plt.clf()
if remove show()
issue goes away.
the correct way access current interactive figure via plt.gcf()
:
plt.gcf().savefig(...)
alternatively, can workaround recreating figure object on each loop:
for in range(0,int(files)): fig= plt.figure() plt.grid(true) ...
Comments
Post a Comment