Python matplotlib地物对象是否可以先进行pickle处理,然后再进行检索?

Python matplotlib地物对象是否可以先进行pickle处理,然后再进行检索?,python,matplotlib,pickle,Python,Matplotlib,Pickle,我正在尝试对matplotlib Figure对象进行pickle处理,以便能够在以后使用x和y数据、标签和标题重新生成图形。这可能吗 尝试使用open and dump进行pickle时,我得到以下回溯: #3rd Party Imports and built-in import random import matplotlib.pyplot as plt import pickle as pk #Initializing lists for x and y values. Volta

我正在尝试对matplotlib Figure对象进行pickle处理,以便能够在以后使用x和y数据、标签和标题重新生成图形。这可能吗

尝试使用open and dump进行pickle时,我得到以下回溯:

#3rd Party Imports and built-in 
import random
import matplotlib.pyplot as plt 
import pickle as pk

#Initializing lists for x and y values. Voltage trim and current measure is our x and y in this case. 
voltage_trim = range(100, 150)
current_meas = []

# A change of parameters modelled by a multiplier in this case
multiplier = range(1,4)

# Initializing lists to store the output current if wanted     
current_storage = []


# Required for Matplotlib
plt.close()
plt.ion() #Required method call in order for interactive plotting to work 

# SPECIFY GRAPH
fig1 = plt.figure()
ax = fig1.add_subplot(1,1,1) # Creates an axis in order to 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 to number of graphs 


# MAIN TEST LOOPS 

for this_mult in multiplier:    

    current_meas = [] # Clears the output list to graph with different multipier 

    #Enumerates input in order to manipulate it below      
    for index, value in enumerate(voltage_trim):  

        #Generating random current values in this case 
        current_meas.append(random.randint(0,10)*this_mult)

        print index ,'Generating results...'
        print index, value

        # Increments index so that lists match dimensiosn and graphing is 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)

        # A pyplot method that pauses the loop, updates the graph and continues to enable for real time graphing, set to small number to be considered insignificant 
        plt.pause(1e-124) 

        # Deletes the real time line to save memory in the loop         
        live_plotting[0].remove()

    # This is the actual storage of plot objects, specify legend label here, and all other arguments the same    
    ax.plot(voltage_trim, current_meas,color = color_choices[this_mult],marker = 'o', label = 'Line'+str(this_mult))     


    #Stores the measured current (A)    
    current_storage.append(current_meas)

    #Calls legend - must be in outer loop     
    plt.legend()

f = open('testt','wb')
pk.dump(fig1, f)
f.close() 
对。试一试

import pickle
import matplotlib.pyplot as plt

file = open('myfile', 'wb')
fig = plt.gcf()
pickle.dump(fig, file)
file.close()
然后阅读

file = open('myfile', 'rb')
pickle.load(file)
plt.show()
file.close()

如果您已经尝试过这个,并且它给您带来了麻烦,请编辑您的问题以添加相关详细信息。您是否安装了模块
gdbm
?@tom我不知道如何检查,也不知道如何安装,文档在这方面非常糟糕。谢谢您的回复。我仍然得到一个与确切的代码错误。我将用回溯更新这个问题。是的,我现在已经在上面指定了。好吧,你的代码对我有效,但我在python3.5上,而你似乎在2.7上。有些东西可能不同,但我不能确切地说是什么。你曾经安装过gdbm吗?我似乎无法安装它。@mariano-不,我没有安装gdbm。虽然我有dbm,但它似乎是python3版本。