Random Access Science

Geophysics, Glaciology, Computer Science and everything inbetween

Saving Matplotlib Figures Using Pickle

Matplotlib is a external library that ships with most bundles of the Python programming language. Providing a huge array of plotting functions is has completely replaced my previous workflow in MATLAB. I have how always missed the *.fig format that exists in MATLAB allowing user to save figures in a native format.

Since version 1.2 matplotlib now allows user to save figures using pickle (Matplotlib: Whats new). Pickle allows python to store most variables directly to a file while retraining the structure. It is similar to the save function in numpy, but more general. It is essentially a dump of python code that can store any kind of information. In this way you can dump the whole matplotlib figure handle into a file and load it on demand. I often work with large datasets that takes time to process. By dumping the figure handle I can quickly make small changes to a figure without processing the raw data behind it.

A Example

The code below illustrates how pickle can be used with python. Using matplotlib a simple sinus function is plotted. Then calling the pickle function dump we save the figure handle to a file called sinus.pickle.

1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np
import matplotlib.pyplot as plt
import pickle as pl

# Plot simple sinus function
fig_handle = plt.figure()
x = np.linspace(0,2*np.pi)
y = np.sin(x)
plt.plot(x,y)

# Save figure handle to disk
pl.dump(fig_handle,file('sinus.pickle','w'))

In a different python script we can load the figure back into python using the load function.

1
2
3
4
5
6
7
import matplotlib.pyplot as plt
import pickle as pl
import numpy as np

# Load figure from disk and display
fig_handle = pl.load(open('sinus.pickle','rb'))
fig_handle.show()

Running the above codeblock through iPython we can make a completely interactive workflow and just save our figures for later if need be. You can even extract the data behind figures,

1
fig_handle.axes[0].lines[0].get_data()

This will return x- and y-coordinates for the sinus function. This also works for 2D plots create with imshow or pcolormesh.

1
fig_handle.axes[0].images[0].get_data()

I use this feature all the time to combine existing plots and store subsets for my data for later processing. Most figures for publication are very different and using pickle I can maintain general plotting scripts and then later combine the data I need.