Skip to content

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

Closed

Description

@cbyrohl

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]

Activity

  1. tacaswell commented on Mar 14, 2017

    @tacaswell
    Member

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

    attn @takluyver

  2. cbyrohl commented on Mar 15, 2017

    @cbyrohl
    Author

    Sure, I reported this in the ipykernel repository.

  3. takluyver commented on Mar 15, 2017

    @takluyver
    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.

  4. tacaswell commented on Jan 12, 2020

    @tacaswell
    Member

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

  5. added this to the v3.2.0 milestone on Jan 12, 2020
  6. tacaswell commented on Jan 20, 2020

    @tacaswell
    Member
  7. brianjoubert commented on Dec 28, 2022

    @brianjoubert

    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'

  8. jklymak commented on Dec 28, 2022

    @jklymak
    Member

    Please open a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions