[英]Pickling Matplotlib plot raising PicklingError: Can't pickle 'RendererAgg' object
I have a program that creates plots - sometimes line plots, sometimes NonUniformImages - using matplotlib. 我有一个程序,使用matplotlib创建绘图 - 有时是线图,有时是NonUniformImages。 I'd like to be able to pickle the plots to reopen them at a later time without going through the whole creation process again.
我希望能够在以后重新打开这些情节,而无需再次完成整个创作过程。 For whatever reason, it keeps throwing a
PicklingError: Can't pickle 'RendererAgg' object
. 无论出于何种原因,它不断抛出一个
PicklingError: Can't pickle 'RendererAgg' object
。 I've tried using both import dill as pickle
and import pickle
, as well as all 4 different pickling options but no change. 我尝试使用
import dill as pickle
和import pickle
,以及所有4种不同的酸洗选项,但没有变化。
The axes are defined here: 轴在此处定义:
class Imaging:
def function:
ax1 = plt.subplot(2,1,1)
ax2 = plt.subplot(2,1,2)
And set here: (Imaging.figureProperties is a list and is meant to hold multiple [ax1,ax2]
objects. Also in the same function as where ax1
and ax2
are defined.) 并在此处设置:( Imaging.figureProperties是一个列表,用于保存多个
[ax1,ax2]
对象。也与定义ax1
和ax2
函数相同。)
Imaging.figureProperties.append([ax1,ax2])
Finally, data is pickled here ( i
is chosen by the user, but it will be within the list): 最后,数据在这里被腌制(
i
是由用户选择的,但它将在列表中):
class2:
with open(filename, 'wb') as f:
pickle.dump(Imaging.figureProperties[i-1],f)
I have no problem running the sample code from this question (with some slight changes such as opening in 'wb'
instead of just 'w'
), as long as I use import dill as pickle
. 只要我使用
import dill as pickle
,我就可以从这个问题中运行示例代码没有问题(只有一些细微的变化,比如在'wb'
而不是'w'
打开)。 If I use the standard import pickle
it throws the same PicklingError
. 如果我使用标准的
import pickle
它会抛出相同的PicklingError
。 What is going on here? 这里发生了什么?
I'm the dill
author. 我是
dill
作者。 If you edit your question to provide code that can be tested, I could better test your code. 如果您编辑问题以提供可以测试的代码,我可以更好地测试您的代码。 I think it might be that you just have typos in your code above -- it should be
def function(self):
. 我想可能是你上面的代码中只有错别字 - 它应该是
def function(self):
Also what is class2:
? 什么是
class2:
I'll just cut to the chase and serialize the thing you are trying to serialize. 我只是切入追逐并序列化你要序列化的东西。 Your code as posted doesn't really make sense.
您发布的代码实际上没有意义。
>>> 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>]
The class you are using is pretty pointless as you are using it, however the code you are asking to serialize does serialize. 当您使用它时,您正在使用的类是毫无意义的,但是您要求序列化的代码会进行序列化。
将Matplotlib更新为1.4.2解决了这些问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.