Python Matplotlib图不';解开
我使用Python2.7,但在将图形保存到pickle时遇到问题 我尝试使用matplotlib图创建dict,并使用标准包对其进行picklePython Matplotlib图不';解开,python,python-2.7,matplotlib,pickle,Python,Python 2.7,Matplotlib,Pickle,我使用Python2.7,但在将图形保存到pickle时遇到问题 我尝试使用matplotlib图创建dict,并使用标准包对其进行pickle import pickle as pkl import matplotlib import matplotlib.pyplot as plt temp = plt.figure() plt.plot([1,2,3],[1,2,3]) to_file = {'figure':temp} with open('test_saving_img', 'wb'
import pickle as pkl
import matplotlib
import matplotlib.pyplot as plt
temp = plt.figure()
plt.plot([1,2,3],[1,2,3])
to_file = {'figure':temp}
with open('test_saving_img', 'wb') as f:
pkl.dump(to_file, f)
del to_file
with open('test_saving_img', 'rb') as f:
to_file = pkl.load(f)
但此代码会引发错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-12-f008d703fb78> in <module>()
6 del to_file
7 with open('test_saving_img', 'rb') as f:
----> 8 to_file = pkl.load(f)
/usr/lib/python2.7/pickle.pyc in load(file)
1376
1377 def load(file):
-> 1378 return Unpickler(file).load()
1379
1380 def loads(str):
/usr/lib/python2.7/pickle.pyc in load(self)
856 while 1:
857 key = read(1)
--> 858 dispatch[key](self)
859 except _Stop, stopinst:
860 return stopinst.value
/usr/lib/python2.7/pickle.pyc in load_build(self)
1215 setstate = getattr(inst, "__setstate__", None)
1216 if setstate:
-> 1217 setstate(state)
1218 return
1219 slotstate = None
/usr/lib/python2.7/dist-packages/matplotlib/figure.pyc in __setstate__(self, state)
1434 allnums = plt.get_fignums()
1435 num = max(allnums) + 1 if allnums else 1
-> 1436 mgr = plt._backend_mod.new_figure_manager_given_figure(num, self)
1437
1438 # XXX The following is a copy and paste from pyplot. Consider
AttributeError: 'module' object has no attribute 'new_figure_manager_given_figure'
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在()
6 del to_文件
7打开时('test_saving_img','rb')为f:
---->8 to_file=pkl.load(f)
/加载(文件)中的usr/lib/python2.7/pickle.pyc
1376
1377 def加载(文件):
->1378返回Unpickler(file.load())
1379
1380 def负载(str):
/加载中的usr/lib/python2.7/pickle.pyc(自)
856而1:
857键=读取(1)
-->858调度[键](自身)
859除_Stop,stopinst外:
860返回stopinst.value
/加载构建中的usr/lib/python2.7/pickle.pyc(自)
1215设置状态=getattr(说明,“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu”,无)
1216如果设置状态:
->1217设置状态(状态)
1218返回
1219 slotstate=无
/usr/lib/python2.7/dist-packages/matplotlib/figure.pyc in____设置状态(self,state)
1434 allnums=plt.get_fignums()
1435 num=最大值(allnums)+1如果allnums为1
->1436 mgr=plt.\u后端\u模块新\u图形\u管理器\u给定\u图形(num,self)
1437
1438#XXX以下是pyplot的复制和粘贴。考虑
AttributeError:'module'对象没有属性'new\u figure\u manager\u given\u figure'
我可以取消勾选并且不丢失数据吗?有没有办法避免这个问题
UPD
我使用pickle(版本为“$Revision:72223$”)和cPickle(版本为1.71)。
我的matplotlib版本是1.5.1
我尝试使用cPickle和pickle,但没有人不为我工作使用python 2.7和matplotlib 2.0,代码运行良好。使用python 2.7.6和matplotlib 1.5.1,代码运行良好。您的pickle和matploblib包是最新的吗?