Skip to content

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

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
Eriz11 opened this issue Mar 4, 2019 · 2 comments
Closed
Labels

Comments

@Eriz11
Copy link

Eriz11 commented Mar 4, 2019

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

@timhoffm
Copy link
Member

timhoffm commented Mar 4, 2019

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.

@timhoffm timhoffm added Community support Users in need of help. Documentation labels Mar 4, 2019
@Eriz11
Copy link
Author

Eriz11 commented Mar 4, 2019

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
Labels
Projects
None yet
Development

No branches or pull requests

3 participants