Skip to content

Fix double picks. #19611

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

Merged
merged 1 commit into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -1726,15 +1726,13 @@ def __init__(self, figure=None):
self._button = None # the button pressed
self._key = None # the key pressed
self._lastx, self._lasty = None, None
self.button_pick_id = self.mpl_connect('button_press_event', self.pick)
self.scroll_pick_id = self.mpl_connect('scroll_event', self.pick)
self.mouse_grabber = None # the axes currently grabbing mouse
self.toolbar = None # NavigationToolbar2 will set me
self._is_idle_drawing = False

@property
def callbacks(self):
return self.figure._canvas_callbacks
callbacks = property(lambda self: self.figure._canvas_callbacks)
button_pick_id = property(lambda self: self.figure._button_pick_id)
scroll_pick_id = property(lambda self: self.figure._scroll_pick_id)

@classmethod
@functools.lru_cache()
Expand Down
4 changes: 4 additions & 0 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2106,6 +2106,10 @@ def __init__(self,
# a proxy property), but that actually need to be on the figure for
# pickling.
self._canvas_callbacks = cbook.CallbackRegistry()
self._button_pick_id = self._canvas_callbacks.connect(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not particularly up on how the callback system works, but this seems intrusive in figure.py.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point of #16220 was to move callback handling from the canvas to the figure (for reasons explained in that PR).

'button_press_event', lambda event: self.canvas.pick(event))
self._scroll_pick_id = self._canvas_callbacks.connect(
'scroll_event', lambda event: self.canvas.pick(event))

if figsize is None:
figsize = mpl.rcParams['figure.figsize']
Expand Down
13 changes: 13 additions & 0 deletions lib/matplotlib/tests/test_backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,19 @@ def test_location_event_position(x, y):
assert re.match("x=foo +y=foo", ax.format_coord(x, y))


def test_pick():
fig = plt.figure()
fig.text(.5, .5, "hello", ha="center", va="center", picker=True)
fig.canvas.draw()
picks = []
fig.canvas.mpl_connect("pick_event", lambda event: picks.append(event))
start_event = MouseEvent(
"button_press_event", fig.canvas, *fig.transFigure.transform((.5, .5)),
MouseButton.LEFT)
fig.canvas.callbacks.process(start_event.name, start_event)
assert len(picks) == 1


def test_interactive_zoom():
fig, ax = plt.subplots()
ax.set(xscale="logit")
Expand Down