numpy - Why does python keep crashing when plotting with matplotlib? -
i running python code simulates , predicts conditions inside rapid compression machine (such temperature, pressure, chemical species, etc.). code set calculates time evolution of these parameters (which saved in numpy arrays) , outputs them function, creates , saves plots of these parameters against time.
these arrays large, , function creates 14 plots using matplotlib. function 7th plot (sometimes more, less), when error reading "python.exe has stopped working". not sure if it's memory issue because plots big or what's going on.
i have tried both (as you'll see in sample code) plt.figure.clf()
, plt.close()
. i've tried doing time.sleep
inbetween plots. following example snippet of function plotting (not real code, example necessary illustrate problem).
i using windows, python 2.7, , matplotlib 1.4.2
def graph_parameters(time, a, b, c, d, e): #a,b,c,d,e parameters delay = 10 plt.figure(1) plt.figure(facecolor="white") plt.plot(time, a) plt.ylabel('a') plt.xlabel('time(s)') plt.savefig('fig1.png') plt.figure(1).clf() plt.close('all') time.sleep(delay) plt.figure(2) plt.figure(facecolor="white") plt.plot(time, b) plt.ylabel('b') plt.xlabel('time(s)') plt.savefig('fig2.png') plt.figure(2).clf() plt.close('all') time.sleep(delay)
etc.
thanks in advance.
i'm not sure, think memory issue. found way (from stackoverflow entry) delete variables don't need. per example above, if want keep 'time_' , parameters 'a_','b_','c_','d_' , 'e_', run following @ end of main code:
graph_array = np.array([time_, a_, b_, c_, d_, e_]) #delete variables aren't going plotted in order free memory attributes = sys.modules[__name__] name in dir(): if name[0]!='_' , np.all( name!= np.array(['graph_array', 'attributes','np', 'gc', 'graph_parameters']) ): delattr(attributes, name) gc.collect() graph_parameters(*graph_array)
basically loop keep private , magical function names , names of specified variables, , deletes other names. got idea answer following link. how clear variables in middle of python script?
i watched task manager code ran, , there significant drop in memory usage (~1,600,000 k ~100,000 k) python right before plots began being saved, indicating method did free memory. also, no crash.
Comments
Post a Comment