Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib save file to be reedited later in ipython

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?

like image 362
user27886 Avatar asked Feb 11 '23 15:02

user27886


1 Answers

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()
like image 140
Esdes Avatar answered Feb 15 '23 11:02

Esdes