Is there a way to save the 'state' of a matplotlib figure so that I may open up the plot and interact with it later, e.g. in a new ipython notebook session.
Plotting pdfs and other file formats doesn't quite do justice if you want to edit the figure later.
This would be useful if you want to annotate or rescale a figure later, but you don't necessarily have all the access to the original script/data that produced the figure.
In matlab, which ipython is often reported to try to emulate in many ways, you can simply save the file as a .fig
or even a script, like .m
(matlab) file! then you reopen your .m
or .fig
in a later matlab session and edit it.
Can this be done with matplotlib?
You can pickle a figure to disk like this
import matplotlib.pyplot as plt
import numpy as np
import pickle
# Plot
fig_object = plt.figure()
x = np.linspace(0,3*np.pi)
y = np.sin(x)
plt.plot(x,y)
# Save to disk
pickle.dump(fig_object,file('sinus.pickle','w'))
And then load it from disk and display:
fig_object = pickle.load(open('sinus.pickle','rb'))
fig_object.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With