Check if figure exists

Hi,

If I have a figure:

h = pp.figure(num=14)

What is the best way to check to see if Figure 14 exists? I'm writing a function that adds plots to a figure window. I want the function to check if the figure exists, and if so, turn off autoscaling (using Eric's suggested axes.set_autoscale_on(False)) in case the user has zoomed.

···

--
Christopher Brown, Ph.D.
Department of Speech and Hearing Science
Arizona State University

Hi Christopher,

I don't know if my suggestion is the best way, but at least it may be
useful ...

regards Matthias

example code:

import matplotlib
import matplotlib.pyplot as plt

for i in range(5)+[14]:
    plt.figure(i) # generating some figures

# get the figure numbers of all existing figures
fig_numbers = [x.num
               for x in matplotlib._pylab_helpers.Gcf.get_all_fig_managers()]

if 14 in fig_numbers:
    print "figure 14 exists"

if plt.figure(14).number in fig_numbers:
    print "figure 14 exists"

···

On Thursday 26 February 2009 20:52:17 Christopher Brown wrote:

Hi,

If I have a figure:

h = pp.figure(num=14)

What is the best way to check to see if Figure 14 exists? I'm writing a
function that adds plots to a figure window. I want the function to
check if the figure exists, and if so, turn off autoscaling (using
Eric's suggested axes.set_autoscale_on(False)) in case the user has zoomed.

Matthias Michler wrote:

Hi,

If I have a figure:

h = pp.figure(num=14)

What is the best way to check to see if Figure 14 exists? I'm writing a
function that adds plots to a figure window. I want the function to
check if the figure exists, and if so, turn off autoscaling (using
Eric's suggested axes.set_autoscale_on(False)) in case the user has zoomed.

Hi Christopher,

I don't know if my suggestion is the best way, but at least it may be useful ...

I think your suggestion is the only way at present, but it suggests that we should provide this capability as part of the API; we don't want to force people to access the private _pylab_helpers module directly.

Eric

···

On Thursday 26 February 2009 20:52:17 Christopher Brown wrote:

regards Matthias

example code:

import matplotlib
import matplotlib.pyplot as plt

for i in range(5)+[14]:
    plt.figure(i) # generating some figures

# get the figure numbers of all existing figures
fig_numbers = [x.num
               for x in matplotlib._pylab_helpers.Gcf.get_all_fig_managers()]

if 14 in fig_numbers:
    print "figure 14 exists"

if plt.figure(14).number in fig_numbers:
    print "figure 14 exists"

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Christopher Brown wrote:

Hi,

If I have a figure:

h = pp.figure(num=14)

What is the best way to check to see if Figure 14 exists? I'm writing a function that adds plots to a figure window. I want the function to check if the figure exists, and if so, turn off autoscaling (using Eric's suggested axes.set_autoscale_on(False)) in case the user has zoomed.

I added two functions to pyplot in svn: fignum_exists(num) and get_fignums() so that with future releases you won't have to delve into _pylab_helpers for this information.

Eric

Eric Firing wrote:

I added two functions to pyplot in svn: fignum_exists(num) and get_fignums() so that with future releases you won't have to delve into _pylab_helpers for this information.

Eric

Wouldn't it be better to have some way to iterate mpl objects, rather than having adhoc functions just for figures? I though there was a standard way to iterate all figures, all axes etc, something like

import matplotlib as mpl

for fig in mpl.Figures:
     ... do stuff

or something similar. Does something like this exist in mpl?

JLS

João Luís Silva wrote:

Eric Firing wrote:

I added two functions to pyplot in svn: fignum_exists(num) and get_fignums() so that with future releases you won't have to delve into _pylab_helpers for this information.

Eric

Wouldn't it be better to have some way to iterate mpl objects, rather than having adhoc functions just for figures? I though there was a standard way to iterate all figures, all axes etc, something like

import matplotlib as mpl

for fig in mpl.Figures:
     ... do stuff

or something similar. Does something like this exist in mpl?

No. The functions I added to pyplot were to provide more information about the figure tracking that pyplot does via the odd Gcf class in _pylab_helpers. This is based on the Matlab practice of having numbered figures.

When the pyplot interface is *not* being used (see examples/api/agg_oo.py and examples/user_interfaces/embedding*.py), a Figure instance is just that--an instance of a class. Tracking the instances is up to the user, who presumably created them. The figure is at the top of the Artist hierarchy. It *does* track axes that are added to it, and you can iterate over those; and those axes track artists that are added to them.

Is there a real need for module-level tracking of Figure instances in this case where pyplot is not being used?

When pyplot is being used, one can always use pyplot.figure(num) to get the Figure object with that number.

Eric