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 |
|
In a different python script we can load the figure back into python using the load function.
1 2 3 4 5 6 7 |
|
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
|
|
This will return x- and y-coordinates for the sinus function. This also works for 2D plots create with imshow or pcolormesh.
1
|
|
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.