3

I often find myself using matplotlib to quickly display data, then later going back and tweaking my plotting code to make pretty figures. In this process, I often use the interactive plot window to adjust things like spacing, zooming, cropping, etc. While I can easily save the resulting figure to an image file, what I really want is to save the sequence of function calls/parameters that produced it.

Note that I don't particularly care to open the same figure again (as in Saving interactive Matplotlib figures). Even something as simple as being able to print the various properties of the figure and axes would be useful.

Community
  • 1
  • 1
perimosocordiae
  • 17,287
  • 14
  • 60
  • 76

2 Answers2

1

While I don't have the answer to your specific question, I'd generally suggest using the Ipython Notebook for these things (and much more!)

Make sure you have %pylab inline in one cell.

When you plot, it will display it in the notebook itself. Then within your cell, just keep experimenting until you have it right (use Ctrl-Enter in the cell). Now the cell will have all the statements you need (and no more!)

The difference between the command line interpreter and the notebook is that the former all statements you typed which leads to a lot of clutter. With the notebook you can edit the line in place.

user1462309
  • 480
  • 2
  • 10
1

A similar question here has an answer I just posted here.

The gist: use MatPlotLib's picklable figure object to save the figure object to a file. See the aforementioned answer for a full example. Here's a shortened example:

fig, ax = matplotlib.pyplot.subplots()
# plot some stuff

import pickle
pickle.dump(  fig,  open('SaveToFile.pickle',  'wb')  )

This does indeed save all plotting tweaks, even those made by the GUI subplot-adjuster. Unpickling via pickle.load() still allows you to interact via CLI or GUI.

Community
  • 1
  • 1
Demis
  • 5,278
  • 4
  • 23
  • 34