Skip to content

Cannot properly reload figure that was previously pickled #3483

Closed

Description

@mdepitta

Dear all,
I just figure out that matplotlib does not have apparently a simple way to save interactive plots as MATLAB "fig" figures. I sincerely hope the "experimental" pickling will be soon consolidated because it would be of great benefit for users imho.

Briefly, I followed the simple procedure outlined here in order to pickle and reload figures:
http://stackoverflow.com/questions/7290370/store-and-reload-matplotlib-pyplot-object

It seems that I am able to pickle the figure indeed, but not to reload it properly.
This is what I get:

Traceback (most recent call last):
  File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_qt4.py", line 299, in resizeEvent
    self.draw()
  File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py", line 154, in draw
    FigureCanvasAgg.draw(self)
  File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 451, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib64/python2.7/site-packages/matplotlib/figure.py", line 1031, in draw
    dsu = [row for row in dsu if not row[1].get_animated()]
  File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 612, in get_animated
    return self._animated
AttributeError: 'AxesSubplot' object has no attribute '_animated'

Any suggestion? Shall I bother about this?
I am using matplotlib 1.3.1 on Fedora 20 with QtAgg backend.

Thanks for your time,

Maurizio

Activity

  1. added this to the v1.4.x milestone on Sep 7, 2014
  2. efiring commented on Sep 7, 2014

    @efiring
    Member

    The bug occurs when the pickle is loaded with mpl in interactive mode, and not otherwise, at least in my limited testing. I think the problem is that in interactive mode, draw() commands are triggered before the unpickling is complete. Conceptually, this could be solved if draw_if_interactive() had a way of knowing that unpickling is in progress; equivalently, a solution would be for the unpickling process to turn off interactive mode until it has finished. Offhand, I don't know how to implement either of these solutions. Does pickle.load set a global variable that serves as a flag to show when loading is in progress?

    If not, then maybe we need to have a pickle.load wrapper in mpl, and tell people to use that instead of raw pickling. As a step beyond that, we could have a pickle.save wrapper, and potentially add metadata to the file.

    @mdepitta in the meantime, a workaround for you is manually turn interactive mode off (plt.ioff()) before loading your pickle, and restore it (plt.ion(), if you were indeed in interactive mode) at the end.

  3. WeatherGod commented on Sep 7, 2014

    @WeatherGod
    Member

    Or, instead of a wrapper around pickle.load(), how about a context manager?

    On Sun, Sep 7, 2014 at 4:06 PM, Eric Firing [email protected]
    wrote:

    The bug occurs when the pickle is loaded with mpl in interactive mode, and
    not otherwise, at least in my limited testing. I think the problem is that
    in interactive mode, draw() commands are triggered before the unpickling
    is complete. Conceptually, this could be solved if draw_if_interactive()
    had a way of knowing that unpickling is in progress; equivalently, a
    solution would be for the unpickling process to turn off interactive mode
    until it has finished. Offhand, I don't know how to implement either of
    these solutions. Does pickle.load set a global variable that serves as a
    flag to show when loading is in progress?

    If not, then maybe we need to have a pickle.load wrapper in mpl, and tell
    people to use that instead of raw pickling. As a step beyond that, we could
    have a pickle.save wrapper, and potentially add metadata to the file.

    @mdepitta https://github.com/mdepitta in the meantime, a workaround for
    you is manually turn interactive mode off (plt.ioff()) before loading
    your pickle, and restore it (plt.ion(), if you were indeed in interactive
    mode) at the end.

    —
    Reply to this email directly or view it on GitHub
    #3483 (comment)
    .

  4. efiring commented on Sep 7, 2014

    @efiring
    Member

    @WeatherGod Yes, a context manager would work. A disadvantage is that context managers are relatively new features of Python, and not something a beginner or casual user (or perhaps an old-timer like myself) is likely to have at their fingertips. With a pair of wrappers we could provide a simple function interface with memorable names (really, if you were a beginner, would you think of using the term "pickle" for saving a plot?), and with the ability to improve functionality via metadata and error handling. In fact, we could add the pickling capability to the savefig method and function, and then add a restorefig function, or something like that.
    I've never been a fan of this general approach; instead I've always advocated using scripts to save code and data as needed to recreate plots. Nevertheless, we have the pickle capability, and there is always demand for this style, so we are stuck with it. Might as well try to make it easy to use.
    An argument to the contrary would be that because pickles only work for short-term storage when one can be sure of restoring into the same environment as that from which one saved, we should let our pickle capability remain obscure; otherwise we might be inundated with complaints that "I can't load the pickle I saved last year." So, maybe we are better off just providing a context manager in pyplot.
    I'm undecided.

  5. tacaswell commented on Sep 8, 2014

    @tacaswell
    Member

    I would advocate against pushing the use of pickle to store/share plots due to the security issues associated with them.

    This will hopefully be addressed by the effort to serialize figures to json.

  6. modified the milestones: , v1.4.x on Sep 27, 2014
  7. modified the milestones: , on Nov 26, 2014
  8. pelson commented on Feb 6, 2015

    @pelson
    Member

    Wow, I'm so behind on my notifications! Thanks for raising this @mdepitta. Can you provide a SSCCE (Short, Self Contained, Correct , Example) to allow us to reproduce? This will dramatically speed up the development of a fix for this issue.

    Thanks!

  9. added 3 commits that reference this issue on Feb 23, 2015
    fdb66ae
    8f605e1
    7740e8c
  10. efiring commented on Feb 28, 2015

    @efiring
    Member

    Given the "won't fix" decision, I'm going to close this.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions