Skip to content

Same environment but different results with saving/loading a pickled figure #13586

Closed

Description

@Eriz11

I faced an issue regarding how to load a figure after pickling it. I don't know if this might be a bug (in VSCode), the very nature of the package or other things.

The saving process of the figure goes smoothly. Code for reference:

with open('cdImg.pickle', 'wb') as f:
                pickle.dump(f1, f)

However, when I load the figure in Ipython with the following code:

In [1]: import pickle

In [2]: import matplotlib.pyplot as plt

In [3]: candleFig = pickle.load(open('cdImg.pickle', 'rb'))

In [4]: candleFig.show() ### Here plt.show() also works. 

The image shows perfectly. Using that same code in VSCode (within the same env; hence, same python and package versions) I don't get the figure, but the program executes perfectly and without any error.

In contrast, if I execute the following in VSCode:

import pickle
import matplotlib.pyplot as plt

def loadImage():
   
   candleFig = pickle.load(open('cdImg.pickle', 'rb'))
       
   plt.show() ### In the IPython example it was: candleFig.show()
   
if __name__ == '__main__':
   
   loadImage()

It loads the image perfectly. Notice the change from candleFig in ipython to plt.show() in VSCode.

¿Is there anything I'm missing about the candleFig.show() not working in VSCode?

Matplotlib version: 3.0.2
Python version: 3.6.8
Ipython: 6.4.0
Pickle version: 4.0

Activity

  1. timhoffm commented on Mar 4, 2019

    @timhoffm
    Member

    Figure.show() is quite different from plt.show(); see e.g. #13101 (comment) For your use case you should use plt.show() in both situations.

    Actually, the environments are not the same. VSCode runs the code in a simple python shell. Ipython does magic in the background. I don't know what exactly, but for sure it's responsible that Figure.show() works in an interactive shell as well.

    The difference between Figure.show() and plt.show() should probably be documented better.

  2. Eriz11 commented on Mar 4, 2019

    @Eriz11
    Author

    Hi Tim,

    Many thanks for the comments. I will stick with the plt.show() option as it looks like (after reading the referenced comment) handles all the caveats for the end-user more easily.

    In regard to the difference in environments, you are totally right. I tried to highlight that the versions were the same in both cases; but my comment was not totally correct technically speaking.

    I could consider this as closed if you (or others) don't have anything else to add.

    Again, thanks for the quick show up.

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