0

I am trying to save figures created with matplotlib as pickle (not sure if other file formats would work) file so that I can reopen the figure and do further modification as needed. However, I would like to see the plot before saving. I found similar queries like: 1. Save python matplotlib figure as pickle, 2. Save plot to image file instead of displaying it using Matplotlib, 3. python matplotlib save graph as data file, 4. Saving interactive Matplotlib figures.

But, none of them calling show() before saving or talks about that.

Here is the code:

import matplotlib.pyplot as plt
import pickle

fig, ax =  plt.subplots()
ax.plot(list(range(1, 100, 10)), 'bo-')
plt.show() # enabling this does not save anything with pickle

# save the mpl figure as pickle format
with open('fig1.pkl', 'wb') as fs:
    pickle.dump(ax, fs)
plt.close("all")

# load the saved figure as mpl figure
with open('fig1.pkl', 'rb') as fl:
    fig1 = pickle.load(fl)

plt.show()

It works just fine if I do not call show() in the first case. But saving seems not working with pickle if I do call. Saving with other formats (.png/.pdf) works fine. Any indication if I am doing any mistakes will be much appreciated.

SCBera
  • 33
  • 1
  • 8
  • 1
    The figure is correctly saved as pickle. The problem is essentially [how-to-show-a-figure-that-has-been-closed](https://stackoverflow.com/questions/31729948/matplotlib-how-to-show-a-figure-that-has-been-closed). Obviously the problem can be circumvented by first pickling the open figure, then call `plt.show()`. – ImportanceOfBeingErnest Sep 18 '19 at 16:17
  • @ImportanceOfBeingErnest: If the first 'plt.show()' is disabled then it works fine as I mentioned even if the figure is closed as it loads and displays again. But if not disabled then I think it is not saving anything. Saving fist and then showing might be the option if there is no alternative. Actually, I wanted to see the goodness of the plot and then to save if suitable. – SCBera Sep 18 '19 at 16:27
  • As said, the figure is correctly saved as pickle (well, not really, because you should of course pickle the figure, not the axes, `dump(fig, fs)`). Then, you can use the workaround in the link. Clearly, this is more complicated than it would need to be, but it's the only solution available. (I might provide a new answer in the link, in case there is an alternative.) – ImportanceOfBeingErnest Sep 18 '19 at 16:46
  • Well, if I use fig instead of ax, I can not load the saved figure which is saved while the first 'show()' is enabled. Otherwise, I can do it even using ax while saving. I will try the option that you provided. Thanks for your help. – SCBera Sep 18 '19 at 17:09
  • What does "I can not load the saved figure" mean? You will in that case load the figure, not the axes of course, but since you will need the figure to show later, that is what you should save. – ImportanceOfBeingErnest Sep 18 '19 at 17:33
  • I meant loading of 'fig1.pkl' file which loads and display correctly if the first 'show()' is OFF. I tried saving the figure first and then loading it and also both saving and opening in a single run. But both produced the same result as I mentioned. – SCBera Sep 18 '19 at 17:47

0 Answers0