instance check, finding figure elements

I'm building plots in stages using several different functions. Since
the figure contains all information, I don't hand handles to
individual elements around.

What's the best way to check for a specific plot element? using
isinstance, or are there specific attributes that could be checked?

For example, I want to add a colorbar to the figure corresponding to
the first axis.imshow:

images = [c for ax in f.axes for c in ax.get_children() if isinstance(c, mpl.image.AxesImage)]
images

[<matplotlib.image.AxesImage object at 0x08C0CAD0>]

f.colorbar(images[0])

<matplotlib.colorbar.Colorbar instance at 0x08E033F0>

f

<matplotlib.figure.Figure object at 0x08B614D0>

example
using recipe http://matplotlib.sourceforge.net/examples/pylab_examples/subplots_adjust.html
https://lh5.googleusercontent.com/-rpw4NHbXvxM/TpmnumYNcRI/AAAAAAAAAK0/5zLnnUPjg0A/corrmatrixgrid.png

Thanks,

Josef

Checkout out Artist.findobj

  http://matplotlib.sourceforge.net/api/artist_api.html#matplotlib.artist.Artist.findobj

Artist is the base class for everything in a matplotlib figure, the
Figure, Axes, Axis, Text, Line2D, Polygon, etc, all derive from
Artist, so you can call this method on any mpl object to recursively
search for objects of a given type. By recursive, I mean search the
objects children, their children, etc. See also

    http://matplotlib.sourceforge.net/users/artists.html

for more info on the artist hierarchy.

But for your specific example, each axes has an `images` attribute,
which is a list of images it contains (this is detailed in the artist
tutorial linked above, so you could simplify your code with something
like:

    images = np.concatenate([ax.images for ax in fig.axes])

JDH

···

On Sat, Oct 15, 2011 at 10:39 AM, <josef.pktd@...287...> wrote:

I'm building plots in stages using several different functions. Since
the figure contains all information, I don't hand handles to
individual elements around.

What's the best way to check for a specific plot element? using
isinstance, or are there specific attributes that could be checked?