anntzer / mplcursors

Interactive data selection cursors for Matplotlib.
https://mplcursors.readthedocs.io
zlib License
113 stars 19 forks source link

Can mplcursors work with mpl.offsetbox.AnnotationBbox #64

Open stepsbystep opened 1 year ago

stepsbystep commented 1 year ago

I'm trying to add hover captions to images added to a plot with AnnotationBbox. I haven't been able to make it work or find anything about how to do it online. With the following code snippet I get the error that follows after it.

`               imtest = plt.imread('sun1.png')
                from matplotlib.offsetbox import OffsetImage, AnnotationBbox
                oi = OffsetImage(imtest, zoom = .15)
                box = AnnotationBbox(oi, (xy[0], xy[1]), frameon=False, label=cob.name)        
                ax.add_artist(box)

        cursor = mplcursors.cursor([box, ax.patches], hover=2) #mplcursors.HoverMode.Transient)

        cursor.connect('add', lambda sel: sel.annotation.set(text=sel.artist.get_label()))
`

(This is just the last few lines.)

--> 259     for canvas in {artist.figure.canvas for artist in artists}]
    261 bindings = {**_default_bindings,
    262             **(bindings if bindings is not None else {})}
    263 unknown_bindings = {*bindings} - {*_default_bindings}

AttributeError: 'ArtistList' object has no attribute 'figure'`

When I only include the AnnotationBbox object in the cursor definition, I get no errors, but I get no hovering cursor text either.

If AnnotationBbox doesn't work with mplcursors, is there some other way to add an image that does?

Mplcursors is great, by the way! I have a lot of uses in mind. If I can get this straightened out, I will be taking up mplcursors functionality with streamlit. They have a great pyplot component, but it doesn't do mplcursors or other hover text yet.

stepsbystep commented 1 year ago

Btw, I also tried the following cursor definition:

cursor = mplcursors.cursor(ax.artists, hover=2) #mplcursors.HoverMode.Transient)

which also resulted in no errors, but no cursor function.

anntzer commented 1 year ago

Thanks for the report. Looks like some work is needed to make that work, which I can't do right now but I'll keep it on my todo. A minimal reproduction example would be appreciated, though.