本文介绍了来自matplotlib的泡菜人物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试从以下问题中重新创建简单的泡菜图示例:保存交互式Matplotlib图,它也来自使用Matplotlib图保存泡菜.但是,当我运行给定的代码时,这些数字似乎可以腌制,但是当我尝试加载该腌制的图形时出现错误.我正在使用Canopy Enthought(v1.6.2.3262),在Python 2.7.3-1上使用Matplotlib 1.5.1-1和Numpy 1.9.2-3来运行它.泡菜代码是:`

I am trying to recreate the simple pickle figure example from the question: Saving interactive Matplotlib figures,which is also sourced from Saving Matplotlib Figures Using Pickle. However, when I run the given codes the figures seems to pickle OK, but then I get an error when I try to load the pickled figure. I am running it using Canopy Enthought (v1.6.2.3262), using Matplotlib 1.5.1-1 and Numpy 1.9.2-3 on Python 2.7.3-1.The pickle code is:`

import numpy as np
import matplotlib.pyplot as plt
import pickle as pl

# Plot simple sinus function
fig_handle = plt.figure()
x = np.linspace(0,2*np.pi)
y = np.sin(x)
plt.plot(x,y)

# Save figure handle to disk
pl.dump(fig_handle,file('sinus.pickle','w'))`

加载该图的代码为:

import matplotlib.pyplot as plt
import pickle as pl
import numpy as np

# Load figure from disk and display
fig_handle = pl.load(open('sinus.pickle','rb'))
fig_handle.show()

我得到的错误是:

%run "Z:\EFNHigh_Res\show_picklefig.py"
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Z:\EFNHigh_Res\show_picklefig.py in <module>()
      4
      5 #plot simple sinus function
----> 6 fig_handle = pl.load(open('Z:\EFNHigh_Res\sinus.pickle','rb'))
      7 fig_handle.show()

C:\Users\Tom\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.6.2.3262.win-x86_64\lib\pickle.pyc in load(file)
   1376
   1377 def load(file):
-> 1378     return Unpickler(file).load()
   1379
   1380 def loads(str):

C:\Users\Tom\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.6.2.3262.win-x86_64\lib\pickle.pyc in load(self)
    856             while 1:
    857                 key = read(1)
--> 858                 dispatch[key](self)
    859         except _Stop, stopinst:
    860             return stopinst.value

C:\Users\Tom\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.6.2.3262.win-x86_64\lib\pickle.pyc in load_global(self)
   1088         module = self.readline()[:-1]
   1089         name = self.readline()[:-1]
-> 1090         klass = self.find_class(module, name)
   1091         self.append(klass)
   1092     dispatch[GLOBAL] = load_global

C:\Users\Tom\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.6.2.3262.win-x86_64\lib\pickle.pyc in find_class(self, module, name)
   1122     def find_class(self, module, name):
   1123         # Subclasses may override this
-> 1124         __import__(module)
   1125         mod = sys.modules[module]
   1126         klass = getattr(mod, name)

ImportError: No module named copy_reg

我知道Python 3和2之间存在差异,因为该文件应该在Python 2的转储中使用而不是open(并且我认为是pickle加载),所以我在代码中尝试了这两种组合.

I know that there is a difference between Python 3 and 2, in that file instead of open should be used in the dump (and I presume the pickle load) for Python 2, so I have tried both combinations in the code.

我不确定错误告诉我什么,所以我无法进一步了解该错误,也无法帮助您理解错误或解决已解决的问题.

I am unsure what the error is telling me, so I haven't been able to get any further with this, any help on understanding the errors or fixing the problem appreciated.

推荐答案

copy_reg的错误是由代码中的腌制图的格式引起的,正确的代码应在写语句中包含wb而不是w,如下所示:在:

The error with copy_reg was being caused by the write format in the code to pickle the figure, the correct code should include wb rather than w in the write statement as in:

# Save figure handle to disk
import pickle
with open('sinus.pickle', 'wb') as f: # should be 'wb' rather than 'w'
    pickle.dump(fig_handle, f)

这是根据copy_reg错误和另一个问题中提供的解决方案确定的 ImportError:没有名为copy_reg pickle的模块关于酸洗时的copy_reg错误.

This was identified based on the copy_reg error and the solution provided in another question ImportError: No module named copy_reg pickle about copy_reg error when pickling.

这篇关于来自matplotlib的泡菜人物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 10:56