Skip to content

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

Closed

Description

@p479h

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.

Activity

  1. anntzer commented on Nov 15, 2024

    @anntzer
    Contributor

    Perhaps related to #29066 too?

  2. p479h commented on Nov 16, 2024

    @p479h
    Author

    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()
  3. anntzer commented on Nov 21, 2024

    @anntzer
    Contributor

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

  4. Kaustbh commented on Dec 3, 2024

    @Kaustbh
    Contributor

    Hi can I work on solving this issue?

  5. p479h commented on Dec 4, 2024

    @p479h
    Author

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

  6. added this to the v3.10.2 milestone on Apr 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions