我有一个使用matplotlib创建绘图的程序--有时是线状图,有时是NonUniformImages。我希望能够在以后重新打开这些情节,而不需要再次经历整个创建过程。不管是什么原因,它总是抛出一个PicklingError: Can't pickle 'RendererAgg' object。我尝试过使用import dill as pickle和import pickle,以及所有4种不同的酸洗选项,但都没有变化。
轴的定义如下:
class Imaging:
def function:
ax1 = plt.subplot(2,1,1)
ax2 = plt.subplot(2,1,2)在这里设置:(Imaging.figureProperties是一个列表,用来容纳多个[ax1,ax2]对象。也在定义ax1和ax2的相同函数中。)
Imaging.figureProperties.append([ax1,ax2])最后,在这里对数据进行酸洗(i由用户选择,但它将在列表中):
class2:
with open(filename, 'wb') as f:
pickle.dump(Imaging.figureProperties[i-1],f)只要我使用this question,在'wb'中运行示例代码就没有问题(只有一些细微的变化,比如在'w'中打开,而不仅仅是在import dill as pickle中打开)。如果我使用标准的import pickle,它会抛出相同的PicklingError。这里发生什么事情?
发布于 2015-02-14 20:48:32
我是dill的作者。如果您编辑您的问题以提供可以测试的代码,我可以更好地测试您的代码。我认为您可能只是在上面的代码中有输入错误--它应该是def function(self):。另外,什么是class2:?我将直接切入主题,并序列化您试图序列化的东西。你发布的代码并没有真正的意义。
>>> import matplotlib.pyplot as plt
>>>
>>> class Imaging:
... def function(self):
... ax1 = plt.subplot(2,1,1)
... ax2 = plt.subplot(2,1,2)
...
>>> Imaging.figureProperties = []
>>>
>>> import dill
>>>
>>> ax1 = plt.subplot(2,1,1)
>>> ax2 = plt.subplot(2,1,2)
>>> Imaging.figureProperties.append([ax1, ax2])
>>> fp = dill.loads(dill.dumps(Imaging.figureProperties[0]))
>>> fp
[<matplotlib.axes._subplots.AxesSubplot object at 0x113085320>, <matplotlib.axes._subplots.AxesSubplot object at 0x113471eb8>]您正在使用的类是没有意义的,因为您正在使用它,但是您请求序列化的代码确实序列化了。
发布于 2015-02-17 21:58:06
将Matplotlib更新到1.4.2解决了这些问题。
https://stackoverflow.com/questions/28507669
复制相似问题