2

I want to save and edit plots generated by matplotlib (not image file, prefer editable file). I used the pickle package and I can save and open a plot by following code.

#-------------
#save code
#-------------

import matplotlib.pyplot as plt
import numpy as np
import pickle

fig = plt.figure('exp_plot')
ax = fig.add_subplot(111)
ax.set_xlim(0, 10)
ax.set_xlabel("x")
ax.set_ylabel("y")

x = np.linspace(0, 10)
y = np.exp(x)
ax.plot(x, y)
pickle.dump(ax, file('exp_plot.pickle', 'wb'))

#-------------
#load code
#-------------

import matplotlib.pyplot as plt
import pickle

fig = pickle.load(open('exp_plot.pickle', 'rb'))
plt.show()

However I can't edit the plot (e.g. change the x axis range, edit the plot title, change the line style) by "Figure options" window of the figure window. When I tried to change the x axis range on the window, the error messages were as follows.

Traceback (most recent call last):
  File "C:\Anaconda2\lib\site-packages\matplotlib\backends\qt_editor\formlayout.py", line 472, in apply
    self.apply_callback(self.formwidget.get())
  File "C:\Anaconda2\lib\site-packages\matplotlib\backends\qt_editor\figureoptions.py", line 124, in apply_callback
    axes.set_yscale(yscale)
  File "C:\Anaconda2\lib\site-packages\matplotlib\axes\_base.py", line 3096, in set_yscale
    self.autoscale_view(scalex=False)
  File "C:\Anaconda2\lib\site-packages\matplotlib\axes\_base.py", line 2198, in autoscale_view
    self.set_ybound(y0, y1)
  File "C:\Anaconda2\lib\site-packages\matplotlib\axes\_base.py", line 2964, in set_ybound
    self.set_ylim(lower, upper, auto=None)
  File "C:\Anaconda2\lib\site-packages\matplotlib\axes\_base.py", line 3055, in set_ylim
    self.callbacks.process('ylim_changed', self)
  File "C:\Anaconda2\lib\site-packages\matplotlib\cbook.py", line 559, in process
    """
AttributeError: 'CallbackRegistry' object has no attribute 'callbacks'

Note I can edit the x axis range and the title by the code, however it's not convenient for me. Do you know how to edit saved plots by "Figure options" window of a figure window.

I use Python 2.7.11, Anaconda 2.4.1 (32-bit), matplotlib 1.5.1 on win32.

user6695701
  • 197
  • 1
  • 10
  • Have you tried pickling the figure instead of the axis? This might not help, since apparently the callbacks are not properly registered, but it might be worth a try. – Jan Christoph Terasa Aug 09 '16 at 16:30
  • 1
    I tried to do this myself a year ago, and it turned out that you can pickle very easy figures, but as soon as you start to add slightly advaced stuff into them they aren't picklable anymore. Also, I would not say it is recommendable to use pickle as a long-term storage of figures, just keep that in mind. This because if a new version of python/pickle/matplotlib/any dependencies are released, old figures might not be loadable anymore. As of now, I would look for an other solution...unfortunately...(I cound not find anyone when I was looking for that last time...). – pathoren Aug 09 '16 at 21:28

0 Answers0