views:

56

answers:

2

I would like to:

pylab.figure()
pylab.plot(x)
pylab.figure()
pylab.plot(y)
# ...
for i, figure in enumerate(pylab.MagicFunctionReturnsListOfAllFigures()):
  figure.savefig('figure%d.png' % i)

What is the magic function that returns a list of current figures in pylab?

Websearch didn't help...

+2  A: 

This should help you (from the pylab.figure doc):

call signature::

figure(num=None, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k')

Create a new figure and return a :class:matplotlib.figure.Figure instance. If num = None, the figure number will be incremented and a new figure will be created.** The returned figure objects have a number attribute holding this number.

If you want to recall your figures in a loop then a good aproach would be to store your figure instances in a list and to call them in the loop.

>> f = pylab.figure()
>> mylist.append(f)
etc...
>> for fig in mylist:
>>     fig.savefig()
joaquin
+3  A: 
import numpy as np
import pylab
import matplotlib._pylab_helpers

x=np.random.random((10,10))
y=np.random.random((10,10))
pylab.figure()
pylab.plot(x)
pylab.figure()
pylab.plot(y)

figures=[manager.canvas.figure
         for manager in matplotlib._pylab_helpers.Gcf.get_all_fig_managers()]
print(figures)

# [<matplotlib.figure.Figure object at 0xb788ac6c>, <matplotlib.figure.Figure object at 0xa143d0c>]

for i, figure in enumerate(figures):
    figure.savefig('figure%d.png' % i)
unutbu
Sure it works, but the underscore in the beginning of _pylab_helpers is a signal that this is not a supported interface and may go away at any time. If there is no published interface and you have a use case for one, please file a feature request.
Jouni K. Seppänen