finding the bounding box of an arbitrary artist

I’m wanting to highlight the artist under the cursor with a transparent Rectangle patch. To do this, I have very, roughly, in a mouse motion handler,

under = self.figure.hitlist(ev)

if under:

artist = under[0]

bbox = artist.get_window_extent()

highlight = matplotlib.patches.Rectangle(xy=bbox.min,width=bbox.width,height=bbox.height,alpha=0.2,color=‘yellow’)

further code to blit the last captured graph region with highlight drawn on top

The main problem is that all artists don’t implement get_window_extent(); a Text object does, and a legend object does, but the Axis objects do not.

So is there a general way to get the bounding box of an artist? I looked through the API and didn’t see anything there. I thought this would be easy to get to, since almost (every?) artist implements contains(). But it looks like in the case of XAxis, anyway, that the “hitbox” is being calculated on the fly in XAxis.contains().

···


Daniel Hyams
dhyams@…287…

Did you ever find a way to get the bounding box of an artist? I could use this too.

Michael.

···

On 7 Oct 2011, at 4:56 PM, Daniel Hyams wrote:

I'm wanting to highlight the artist under the cursor with a transparent Rectangle patch. To do this, I have very, roughly, in a mouse motion handler,

under = self.figure.hitlist(ev)
if under:
   artist = under[0]
   bbox = artist.get_window_extent()
   highlight = matplotlib.patches.Rectangle(xy=bbox.min,width=bbox.width,height=bbox.height,alpha=0.2,color='yellow')
   # further code to blit the last captured graph region with highlight drawn on top

The main problem is that all artists don't implement get_window_extent(); a Text object does, and a legend object does, but the Axis objects do not.

So is there a general way to get the bounding box of an artist?

No, I didn’t… I believe that in this specific case, I just manually drew a decently sized box around the axis spine and implemented my own contains method to match.

It would be nice for every artist to be consistent in this area though. Maybe a MEP is in order to make every artist have a consistent interface as defined by the base class?

···

On Dec 30, 2012 1:00 AM, “Michael McNeil Forbes” <michael.forbes+python@…287…> wrote:

On 7 Oct 2011, at 4:56 PM, Daniel Hyams wrote:

I’m wanting to highlight the artist under the cursor with a transparent Rectangle patch. To do this, I have very, roughly, in a mouse motion handler,

under = self.figure.hitlist(ev)

if under:

artist = under[0]

bbox = artist.get_window_extent()

highlight = matplotlib.patches.Rectangle(xy=bbox.min,width=bbox.width,height=bbox.height,alpha=0.2,color=‘yellow’)

further code to blit the last captured graph region with highlight drawn on top

The main problem is that all artists don’t implement get_window_extent(); a Text object does, and a legend object does, but the Axis objects do not.

So is there a general way to get the bounding box of an artist?

Did you ever find a way to get the bounding box of an artist? I could use this too.

Michael.