Search code examples
python-2.7matplotlibpicklefigure

Pickle figures from matplotlib: 2


Following Pickle figures from matplotlib, I am trying to load a figure from a pickle. I am using the same code with the modifications that are suggested in the responses. Saving script:

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

# Plot simple sinus function
fig_handle = plt.figure()
x = np.linspace(0,2*np.pi)
y = np.sin(x)
plt.plot(x,y)
# plt.show()

# Save figure handle to disk
pl.dump(fig_handle,file('sinus.pickle','wb'))

Loading script:

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

# Load figure from disk and display
fig_handle = pl.load(open('sinus.pickle', 'rb'))
fig_handle.show()

The saving script produces a file named "sinus.pickle" but the loading file does not show the anticipated figure. Any suggestions?

Python 2.7.13 matplotlib 2.0.0 numpy 1.12.1

p.s. following a suggestion replaced fig_handle.show() with pat.show() which produced an error: Traceback (most recent call last):

File "/usr/local/lib/python2.7/
   site-packages/matplotlib/backends/backend_macosx.py", line 109, 
   in_set_device_scale
   self.figure.dpi = self.figure.dpi / self._device_scale * value
   File "/usr/local/lib/python2.7/site-packages/matplotlib/figure.py", 
   line  416, in _set_dpi
   self.callbacks.process('dpi_changed', self)
   File "/usr/local/lib/python2.7/site-packages/matplotlib/cbook.py", 
   line  546, in process
   if s in self.callbacks:
   AttributeError: 'CallbackRegistry' object has no attribute 'callbacks'

Solution

  • Final solution included modification of fig_handle.show() to plt.show() and modification of the backend to "TkAgg", based to an advice given by ImportanceOfBeingErnest