来自 matplotlib 的泡菜图
Pickle figures from matplotlib
我正在尝试根据问题重新创建简单的泡菜图示例: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。
泡菜代码是:`
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 的转储(并且我假设 pickle 加载)中应该使用该文件而不是 open,所以我在代码中尝试了两种组合。
我不确定错误告诉我的是什么,所以我无法进一步了解这个问题,感谢任何帮助理解错误或解决问题的帮助。
copy_reg 的错误是由代码中用于 pickle 图形的写入格式引起的,正确的代码应该在写入语句中包含 wb 而不是 w,如:
# 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: No module named copy_reg pickle 中关于酸洗时 copy_reg 错误的解决方案确定的。
我正在尝试根据问题重新创建简单的泡菜图示例: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。 泡菜代码是:`
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 的转储(并且我假设 pickle 加载)中应该使用该文件而不是 open,所以我在代码中尝试了两种组合。
我不确定错误告诉我的是什么,所以我无法进一步了解这个问题,感谢任何帮助理解错误或解决问题的帮助。
copy_reg 的错误是由代码中用于 pickle 图形的写入格式引起的,正确的代码应该在写入语句中包含 wb 而不是 w,如:
# 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: No module named copy_reg pickle 中关于酸洗时 copy_reg 错误的解决方案确定的。