Skip to content

Fix for #9608: RectangleSelector now reports mouse button press and release events correctly #20611

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

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
6 changes: 6 additions & 0 deletions doc/api/next_api_changes/behavior/20611-MRR.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
``RectangleSelector`` ``onselect`` now reports actual mouse press and release coordinates
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The current docstrings in `.RectangleSelector` describe ``onselect`` as a function with the
the signature ``def onselect(eclick: MouseEvent, erelease: MouseEvent)``. The
previous behaviour reported the ``extent`` of the rectangle, not the mouse event
coordinates. The new behaviour reports the actual mouse event coordinates.
4 changes: 2 additions & 2 deletions lib/matplotlib/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def onselect(epress, erelease):
ax._got_onselect = True
assert epress.xdata == 100
assert epress.ydata == 100
assert erelease.xdata == 199
assert erelease.ydata == 199
assert erelease.xdata == 200
assert erelease.ydata == 200

tool = widgets.RectangleSelector(ax, onselect, **kwargs)
do_event(tool, 'press', xdata=100, ydata=100, button=1)
Expand Down
23 changes: 5 additions & 18 deletions lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2701,7 +2701,6 @@ def _press(self, event):
if self._active_handle is None:
x = event.xdata
y = event.ydata
self.visible = False
self.extents = x, x, y, y
self.visible = True
else:
Expand All @@ -2714,25 +2713,13 @@ def _release(self, event):
if not self._interactive:
self._to_draw.set_visible(False)

# update the eventpress and eventrelease with the resulting extents
x0, x1, y0, y1 = self.extents
self._eventpress.xdata = x0
self._eventpress.ydata = y0
xy0 = self.ax.transData.transform([x0, y0])
self._eventpress.x, self._eventpress.y = xy0

self._eventrelease.xdata = x1
self._eventrelease.ydata = y1
xy1 = self.ax.transData.transform([x1, y1])
self._eventrelease.x, self._eventrelease.y = xy1

# calculate dimensions of box or line
if self.spancoords == 'data':
spanx = abs(self._eventpress.xdata - self._eventrelease.xdata)
spany = abs(self._eventpress.ydata - self._eventrelease.ydata)
spanx = abs(self._eventpress.xdata - event.xdata)
spany = abs(self._eventpress.ydata - event.ydata)
elif self.spancoords == 'pixels':
spanx = abs(self._eventpress.x - self._eventrelease.x)
spany = abs(self._eventpress.y - self._eventrelease.y)
spanx = abs(self._eventpress.x - event.x)
spany = abs(self._eventpress.y - event.y)
else:
_api.check_in_list(['data', 'pixels'],
spancoords=self.spancoords)
Expand All @@ -2747,7 +2734,7 @@ def _release(self, event):
return

# call desired function
self.onselect(self._eventpress, self._eventrelease)
self.onselect(self._eventpress, event)
self.update()

return False
Expand Down