Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can mplcursors work with mpl.offsetbox.AnnotationBbox #64

Open
stepsbystep opened this issue Jul 18, 2023 · 2 comments
Open

Can mplcursors work with mpl.offsetbox.AnnotationBbox #64

stepsbystep opened this issue Jul 18, 2023 · 2 comments

Comments

@stepsbystep
Copy link

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
Copy link
Author

stepsbystep commented Jul 18, 2023

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
Copy link
Owner

anntzer commented Jul 20, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants