我在嘗試從問題中重新創建簡單的泡菜圖示例:Saving interactive Matplotlib figures, 也來自Saving Matplotlib Figures Using Pickle。但是,當我運行給定的代碼時,這些數字看起來好像沒問題,但是當我嘗試加載醃製的數字時,出現錯誤。我使用Canopy Enthought(v1.6.2.3262)運行它,在Python 2.7.3-1上使用Matplotlib 1.5.1-1和Numpy 1.9.2-3。 鹹菜代碼:`matplotlib的泡菜數字
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和Python 2之間,在該文件中,而不是打開的文件應該用於Python 2的轉儲文件(並且我假設pickle加載),所以我已經在代碼中嘗試了兩種組合。
我不確定錯誤告訴我什麼,所以我一直無法得到任何進一步的幫助,理解錯誤或解決問題的讚賞。
嗯,它看起來可能是一個微妙的錯誤...嘗試'在您加載'前導入copy_reg'。如果這有效,那麼你應該將錯誤報告給'matplotlib'。然後,我會希望'dill'和/或'cloudpickle'能正確處理對象,而無需進行額外的導入。 –
感謝邁克,我嘗試過,但它沒有奏效,但是它導致我進一步調查copy_reg錯誤,並且我發現我需要在初始pickle中明確寫入二進制文件才能使其正常工作(正如我使用的只有w,而不是wb),如[ImportError:No module named copy_reg pickle]中所述(http://stackoverflow.com/questions/556269/importerror-no-module-named-copy-reg-pickle)。不確定在stackoverflow協議 - 我應該上傳我自己的答案嗎? –
啊,這是有道理的......你應該回答自己的問題。 –