【问题标题】:Can a matplotlib figure object be pickled then retrieved?可以腌制然后检索 matplotlib 图形对象吗?
【发布时间】:2016-07-11 10:31:30
【问题描述】:

我正在尝试腌制一个 matplotlib Figure 对象,以便以后能够使用 x 和 y 数据以及标签和标题重新生成图形。这可能吗?

当尝试使用 open 和 dump 来泡菜时,我得到了这个回溯:

#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() 

【问题讨论】:

  • 如果您已经尝试过,但它给您带来了麻烦,请编辑您的问题以添加相关详细信息。
  • 你有没有安装gdbm这个模块?
  • @tom 我不确定如何检查如何安装它,文档对此非常糟糕。

标签: python matplotlib pickle


【解决方案1】:

是的。试试

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()

【讨论】:

  • 感谢您的回复。我仍然收到确切代码的错误。我将使用回溯更新问题。
  • 是的,我已经在上面指定了。
  • 好吧,你的代码对我有用,但我在 python3.5 上,而你似乎在 2.7 上。有些东西可能会有所不同,但我不能确切地说出是什么。
  • 你曾经安装过 gdbm 吗?我似乎无法 pip 安装它。
  • @mariano - 不。我没有安装 gdbm。我有 dbm 虽然它似乎是 python3 版本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-14
  • 2020-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多