【问题标题】:Pickling Matplotlib plot raising PicklingError: Can't pickle 'RendererAgg' object腌制 Matplotlib 情节引发 PicklingError:不能腌制 'RendererAgg' 对象
【发布时间】:2015-02-13 19:52:54
【问题描述】:

我有一个使用 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] 对象。也与定义ax1ax2 的函数相同。)

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

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

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

只要我使用import dill as pickle,我从this question 运行示例代码没有问题(有一些细微的变化,例如在'wb' 而不是仅'w' 中打开)。如果我使用标准import pickle,它会抛出相同的PicklingError。这是怎么回事?

【问题讨论】:

  • 什么版本的mpl?我想我们最近清理了很多这些东西。
  • @tcaswell,我使用的是 1.4.0。刚刚更新到 1.4.2。

标签: python matplotlib pickle figure dill


【解决方案1】:

我是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>]

您使用的类在使用时毫无意义,但是您要求序列化的代码确实会序列化。

【讨论】:

  • 我只是想展示一般结构,因为我拥有的代码相当混乱,但我现在意识到我应该首先将它们全部上传。将编辑问题。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-04
    • 2016-03-27
    • 2018-10-30
    • 2022-07-16
    • 2021-08-18
    • 2020-06-26
    • 1970-01-01
    • 2019-12-14
    相关资源
    最近更新 更多