Store and reload matplotlib.pyplot object

后端 未结 5 878
别跟我提以往
别跟我提以往 2020-11-28 03:50

I work in an psudo-operational environment where we make new imagery on receipt of data. Sometimes when new data comes in, we need to re-open an image and update that image

相关标签:
5条回答
  • 2020-11-28 04:03

    Did you try the pickle module? It serialises an object, dumps it to a file, and can reload it from the file later.

    0 讨论(0)
  • 2020-11-28 04:03

    I'm also having problems with this in Python 3.7 and Matplotlib 3.1.3.

    I save my figures as (simplified code):

    fig, ax = plt.subplots()
    fig.show() #works fine
    list_figs.append(fig)
    output = {
        "figures": list_figs
    }
    with open( f"mPair.pkl", "wb" ) as f:
        pickle.dump( output, f )
    res = pickle.load( open( f"mPair.pkl", "rb" ) )
    res["figures"][0].show() #does not work
    

    The code works fine if I directly show the figure but after pickling/unpickling I get:

    Traceback (most recent call last):
      File "/Users/xx/opt/anaconda3/envs/nengo3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3331, in run_code
        exec(code_obj, self.user_global_ns, self.user_ns)
      File "<ipython-input-3-4759ba430504>", line 1, in <module>
        res1[ "fig_post" ].show()
      File "/Users/xx/opt/anaconda3/envs/nengo3/lib/python3.7/site-packages/matplotlib/figure.py", line 438, in show
        "created by pyplot.figure()." % err)
    AttributeError: 'NoneType' object has no attribute 'manager'
    Figure.show works only for figures managed by pyplot, normally created by pyplot.figure()
    
    0 讨论(0)
  • 2020-11-28 04:08

    As of 1.2 matplotlib ships with experimental pickling support. If you come across any issues with it, please let us know on the mpl mailing list or by opening an issue on github.com/matplotlib/matplotlib

    HTH

    EDIT: Added a simple example

    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)
    pickle.dump(ax, file('myplot.pickle', 'w'))
    

    Then in a separate session:

    import matplotlib.pyplot as plt
    import pickle
    
    ax = pickle.load(file('myplot.pickle'))
    plt.show()
    
    0 讨论(0)
  • 2020-11-28 04:09

    I produced figures for a number of papers using matplotlib. Rather than thinking of saving the figure (as in MATLAB), I would write a script that plotted the data then formatted and saved the figure. In cases where I wanted to keep a local copy of the data (especially if I wanted to be able to play with it again) I found numpy.savez() and numpy.load() to be very useful.

    At first I missed the shrink-wrapped feel of saving a figure in MATLAB, but after a while I have come to prefer this approach because it includes the data in a format that is available for further analysis.

    0 讨论(0)
  • 2020-11-28 04:26

    A small modification to Pelson's answer for people working on a Jupyterhub

    Use %matplotlib notebook before loading the pickle. Using %matplotlib inline did not work for me in either jupyterhub or jupyter notebook. and gives a traceback ending in AttributeError: 'module' object has no attribute 'new_figure_manager_given_figure'.

    import matplotlib.pyplot as plt
    import numpy as np
    import pickle
    
    %matplotlib notebook
    
    ax = plt.subplot(111)
    x = np.linspace(0, 10)
    y = np.exp(x)
    plt.plot(x, y)
    with open('myplot.pkl','wb') as fid:
        pickle.dump(ax, fid)
    

    Then in a separate session:

    import matplotlib.pyplot as plt
    import pickle
    
    %matplotlib notebook
    
    with open('myplot.pkl','rb') as fid:
        ax = pickle.load(fid)
    plt.show()
    
    0 讨论(0)
提交回复
热议问题