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>
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
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?