8000 [MNT]: Draggable legend gets stuck on cursor after scroll event · Issue #29142 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

[MNT]: Draggable legend gets stuck on cursor after scroll event #29142

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

Closed
p479h opened this issue Nov 14, 2024 · 5 comments · Fixed by #29842
Closed

[MNT]: Draggable legend gets stuck on cursor after scroll event #29142

p479h opened this issue Nov 14, 2024 · 5 comments · Fixed by #29842
Labels
status: confirmed bug status: has patch patch suggested, PR still needed
Milestone

Comments

@p479h
Copy link
p479h commented Nov 14, 2024

Summary

Draggable legend gets stuck to cursor after scrolling while hovering the artist. To 'unstick' it, it is necessary to left click. I believe the expected behavior is to only drag the legend while holding the left mouse button. If that is not the case, please close this issue.

Proposed fix

Sample code:

import matplotlib.pyplot as plt

plt.plot([],[], label='scroll over me, then move the cursor')
plt.legend(draggable=True)
plt.show()

I believe the issue is that the class DraggableBase (at line 1425 of matplotlib/offsetbox.py) uses a 'pick' event to grab the artist, which I believe includes scrolling events. It might be possible to return from a false positive pick by checking that the pick event is a mouse click instead of scrolling at DraggableBase.on_pick. Another alternative is to change the pick event to a button_press event.

@anntzer
Copy link
Contributor
anntzer commented Nov 15, 2024

Perhaps related to #29066 too?

@p479h
Copy link
Author
p479h commented Nov 16, 2024

I am not sure to be honest. I don't know much about the backends. But the problem described doesn't appear to be the same. In fact I tried implementing my solution inside offsetbox.py and it worked as predicted. Scroll events no longer make legend stick to cursor.

This is the proposed modification (first 3 lines of DraggableBase.on_pick):

def on_pick(self, evt):
       # Return if mouseevent is a scroll (this is the modification)
       if evt.mouseevent.button in ('up','down'):
           return
       if self._check_still_parented() and evt.artist == self.ref_artist:
           self.mouse_x = evt.mouseevent.x
           self.mouse_y = evt.mouseevent.y
           self.got_artist = True
           if self._use_blit:
               self.ref_artist.set_animated(True)
               self.canvas.draw()
               self.background = \
                   self.canvas.copy_from_bbox(self.ref_artist.figure.bbox)
               self.ref_artist.draw(
                   self.ref_artist.figure._get_renderer())
               self.canvas.blit()
           self.save_offset()

@anntzer
Copy link
Contributor
anntzer commented Nov 21, 2024

Oh, I see. OK, I agree your proposed patch seems reasonable (perhaps rather check evt.name == "scroll_event", which is more explicit, though).

@anntzer anntzer added status: confirmed bug status: has patch patch suggested, PR still needed and removed Maintenance labels Nov 21, 2024
@Kaustbh
Copy link
Contributor
Kaustbh commented Dec 3, 2024

Hi can I work on solving this issue?

@p479h
Copy link
Author
8000 p479h commented Dec 4, 2024

Is the thread author responsible for giving permission? If so, yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: confirmed bug status: has patch patch suggested, PR still needed
Projects
None yet
4 participants
0