Skip to content

Unable to pickle.load(fig) with mpl in jupyter notebook #8291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cbyrohl opened this issue Mar 14, 2017 · 7 comments
Closed

Unable to pickle.load(fig) with mpl in jupyter notebook #8291

cbyrohl opened this issue Mar 14, 2017 · 7 comments
Milestone

Comments

@cbyrohl
Copy link

cbyrohl commented Mar 14, 2017

I use the following code snippet, similar to issue #3986 on Py35/matplotlib2.0.0 installed via Anaconda3:

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



ax = plt.subplot(111)
x = np.linspace(0, 10)
y = np.exp(x)
plt.plot(x, y)
with open('bla.pickle', 'wb') as pickle_file:
    pickle.dump(ax, pickle_file)
import matplotlib.pyplot as plt
import pickle

with open('bla.pickle', 'rb') as pickle_file:
    ax = pickle.load(pickle_file)
plt.show()

The latter pickle.load() fails with:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-23-0e84ab5169e3> in <module>()
     15 
     16 with open('bla.pickle', 'rb') as pickle_file:
---> 17     ax = pickle.load(pickle_file)
     18 plt.show()

/home/uni09/cosmo/cbyrohl/anaconda3/envs/py35/lib/python3.5/site-packages/matplotlib/figure.py in __setstate__(self, state)
   1443             allnums = plt.get_fignums()
   1444             num = max(allnums) + 1 if allnums else 1
-> 1445             mgr = plt._backend_mod.new_figure_manager_given_figure(num, self)
   1446 
   1447             # XXX The following is a copy and paste from pyplot. Consider

AttributeError: module 'ipykernel.pylab.backend_inline' has no attribute 'new_figure_manager_given_figure'

This code was executed in a jupyter notebook (ipython 5.3)..

When executing as usual python script, it works just fine. It also works fine when dumping in Jupyter and loading in script.

[TAC edited to add markup]

@tacaswell
Copy link
Member

This should be reported against IPython as they maintain the inline backend.

attn @takluyver

@cbyrohl
Copy link
Author

cbyrohl commented Mar 15, 2017

Sure, I reported this in the ipykernel repository.

@takluyver
Copy link
Contributor

@tacaswell is it easy to implement the new_figure_manager_given_figure function? I'm not familiar with what's required for a mpl backend.

@tacaswell
Copy link
Member

3 years on I finally came back to this...

@tacaswell tacaswell added this to the v3.2.0 milestone Jan 12, 2020
@tacaswell
Copy link
Member

tacaswell commented Jan 20, 2020

Closed by #16189 and ipython/ipykernel#476

martinRenou pushed a commit to ipython/matplotlib-inline that referenced this issue Feb 11, 2021
@brianjoubert
Copy link

brianjoubert commented Dec 28, 2022

Hello,

Is it possible to dump the matplotlib plot in Pickle using the default figure manager and load it in Jupyter notebook.
Basically I tried now to save a plot by running a .py file

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

x = np.linspace(-np.pi, np.pi)
y = np.cos(x)
fig, ax = plt.subplots(1, 1)
ax.plot(x, y, label='cos(x)')
ax.set_xlabel('Time (s)')

pickle.dump((fig, ax), open('myplot.pickle', 'wb'))
`

and then load the produced pickle in a Jupyter Notebook:

`
import matplotlib.pyplot as plt
import pickle

fig, ax = pickle.load(open('myplot.pickle', 'rb'))
ax.set_ylabel('Voltage (mV)')
ax.minorticks_on()
ax.legend()
`

I always get the following message.

AttributeError: 'NoneType' object has no attribute 'new_figure_manager_given_figure'

@jklymak
Copy link
Member

jklymak commented Dec 28, 2022

Please open a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants