Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store and reload matplotlib.pyplot object

I work in an psudo-operational environment where we make new imagery on receipt of data. Sometimes when new data comes in, we need to re-open an image and update that image in order to create composites, add overlays, etc. In addition to adding to the image, this requires modification of titles, legends, etc.

Is there something built into matplotlib that would let me store and reload my matplotlib.pyplot object for later use? It would need to maintain access to all associated objects including figures, lines, legends, etc. Maybe pickle is what I'm looking for, but I doubt it.

like image 660
Vorticity Avatar asked Sep 03 '11 00:09

Vorticity


People also ask

How do you save a plot of an object in Python?

To save plot figure as JPG or PNG file, call savefig() function on matplotlib. pyplot object. Pass the file name along with extension, as string argument, to savefig() function.

What is Pyplot CLF ()?

The clf() function in pyplot module of matplotlib library is used to clear the current figure.

How do you save a Pyplot in Python?

Now if you want to save matplotlib figures as image files programmatically, then all you need is matplotlib. pyplot. savefig() function. Simply pass the desired filename (and even location) and the figure will be stored on your disk.

What does PLT Tight_layout () do?

The tight_layout() function in pyplot module of matplotlib library is used to automatically adjust subplot parameters to give specified padding.


Video Answer


1 Answers

As of 1.2 matplotlib ships with experimental pickling support. If you come across any issues with it, please let us know on the mpl mailing list or by opening an issue on github.com/matplotlib/matplotlib

HTH

EDIT: Added a simple example

import matplotlib.pyplot as plt import numpy as np import pickle  ax = plt.subplot(111) x = np.linspace(0, 10) y = np.exp(x) plt.plot(x, y) pickle.dump(ax, file('myplot.pickle', 'w')) 

Then in a separate session:

import matplotlib.pyplot as plt import pickle  ax = pickle.load(file('myplot.pickle')) plt.show() 
like image 159
pelson Avatar answered Oct 06 '22 00:10

pelson