Matplotlib图不会破坏

时间:2017-02-21 16:58:59

标签: python python-2.7 matplotlib pickle

我使用python 2.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') 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'

我可以打开它并且不会丢失我的数据吗?有没有办法避免这个问题?

UPD

我同时使用pickle(版本'$ Revision:72223 $')和cPickle(版本1.71)。 我的matplotlib版本是1.5.1 我尝试使用cPickle和pickle,没有人不工作

0 个答案:

没有答案