python - Can a matplotlib figure object be pickled then retrieved? -
i trying pickle matplotlib figure object able regenerate graph x , y data , labels , title @ later time. possible?
when trying use open , dump pickle traceback:
#3rd party imports , built-in import random import matplotlib.pyplot plt import pickle pk #initializing lists x , y values. voltage trim , current measure our x , y in case. voltage_trim = range(100, 150) current_meas = [] # change of parameters modelled multiplier in case multiplier = range(1,4) # initializing lists store output current if wanted current_storage = [] # required matplotlib plt.close() plt.ion() #required method call in order interactive plotting work # specify graph fig1 = plt.figure() ax = fig1.add_subplot(1,1,1) # creates axis in order have multiple lines plt.title('voltage trim vs current \nsome fancy sub-title here') plt.xlabel('voltage trim / v') plt.ylabel('current measured/ a') plt.grid(true) color_choices = ['k', 'g','r','b','k','c', 'm', 'y'] # add more according number of graphs # main test loops this_mult in multiplier: current_meas = [] # clears output list graph different multipier #enumerates input in order manipulate below index, value in enumerate(voltage_trim): #generating random current values in case current_meas.append(random.randint(0,10)*this_mult) print index ,'generating results...' print index, value # increments index lists match dimensiosn , graphing possible index += 1 # optional real time plotting function, comment out if not wanted live_plotting = ax.plot(voltage_trim[:index], current_meas, color = color_choices[this_mult])#,label = 'line'+str(this_mult) # pyplot method pauses loop, updates graph , continues enable real time graphing, set small number considered insignificant plt.pause(1e-124) # deletes real time line save memory in loop live_plotting[0].remove() # actual storage of plot objects, specify legend label here, , other arguments same ax.plot(voltage_trim, current_meas,color = color_choices[this_mult],marker = 'o', label = 'line'+str(this_mult)) #stores measured current (a) current_storage.append(current_meas) #calls legend - must in outer loop plt.legend() f = open('testt','wb') pk.dump(fig1, f) f.close()
yes. try
import pickle import matplotlib.pyplot plt file = open('myfile', 'wb') fig = plt.gcf() pickle.dump(fig, file) file.close()
then read
file = open('myfile', 'rb') pickle.load(file) plt.show() file.close()
Comments
Post a Comment