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.