1

我有一个使用 matplotlib 创建绘图的程序 - 有时是线图,有时是 NonUniformImages。我希望能够腌制这些情节以便稍后重新打开它们,而无需再次经历整个创建过程。无论出于何种原因,它一直在抛出一个PicklingError: Can't pickle 'RendererAgg' object. 我试过同时使用import dill as pickleimport pickle,以及所有 4 种不同的酸洗选项,但没有改变。

轴在此处定义:

class Imaging:
    def function:
        ax1 = plt.subplot(2,1,1)
        ax2 = plt.subplot(2,1,2)

并在此处设置:(Imaging.figureProperties 是一个列表,用于保存多个[ax1,ax2]对象。也在与 whereax1ax2定义相同的函数中。)

Imaging.figureProperties.append([ax1,ax2])

最后,数据在这里被腌制(i由用户选择,但它将在列表中):

class2:
    with open(filename, 'wb') as f:
        pickle.dump(Imaging.figureProperties[i-1],f)

只要使用. _ _ 如果我使用标准,它会抛出相同的. 这里发生了什么?'wb''w'import dill as pickleimport picklePicklingError

4

2 回答 2

2

我是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-14T12:48:31.600 回答
1

将 Matplotlib 更新到 1.4.2 解决了这些问题。

于 2015-02-17T13:58:05.607 回答