1

I have a program that creates plots - sometimes line plots, sometimes NonUniformImages - using matplotlib. 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. I've tried using both import dill as pickle and import pickle, as well as all 4 different pickling options but no change.

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.append([ax1,ax2])

Finally, data is pickled here (i is chosen by the user, but it will be within the list):

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. If I use the standard import pickle it throws the same PicklingError. What is going on here?

Community
  • 1
  • 1
wes3449
  • 305
  • 1
  • 2
  • 10

2 Answers2

2

I'm the dill author. 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):. Also what is 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.

Mike McKerns
  • 33,715
  • 8
  • 119
  • 139
  • I just meant to show the general structure as the code I have is rather messy, but I realize now that I should have just uploaded it all in the first place. Will edit the question. – wes3449 Feb 17 '15 at 13:48
1

Updating Matplotlib to 1.4.2 solved the problems.

wes3449
  • 305
  • 1
  • 2
  • 10