From 2298816c8f5234bda8d0fb1e6430cda3de53df8a Mon Sep 17 00:00:00 2001 From: David Stansby Date: Fri, 7 Jan 2022 18:41:19 +0000 Subject: [PATCH 1/2] Factor out logic for minspan{x,y} --- lib/matplotlib/widgets.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index ffaa35ea8924..73822ac0d8a9 100644 --- a/lib/matplotlib/widgets.py +++ b/lib/matplotlib/widgets.py @@ -3005,6 +3005,19 @@ def _press(self, event): return False + def _selector_too_small(self): + # Return True if selector is smaller than minimum allowed size + if self.spancoords == 'data': + spanx = self._eventpress.xdata - self._eventrelease.xdata + spany = self._eventpress.ydata - self._eventrelease.ydata + elif self.spancoords == 'pixels': + spanx = self._eventpress.x - self._eventrelease.x + spany = self._eventpress.y - self._eventrelease.y + else: + _api.check_in_list(['data', 'pixels'], + spancoords=self.spancoords) + return abs(spanx) <= self.minspanx or abs(spany) <= self.minspany + def _release(self, event): """Button release event handler.""" if not self._interactive: @@ -3026,20 +3039,7 @@ def _release(self, event): 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) - elif self.spancoords == 'pixels': - spanx = abs(self._eventpress.x - self._eventrelease.x) - spany = abs(self._eventpress.y - self._eventrelease.y) - else: - _api.check_in_list(['data', 'pixels'], - spancoords=self.spancoords) - # check if drawn distance (if it exists) is not too small in - # either x or y-direction - minspanxy = (spanx <= self.minspanx or spany <= self.minspany) - if (self._drawtype != 'none' and minspanxy): + if self._drawtype != 'none' and self._selector_too_small(): for artist in self.artists: artist.set_visible(False) if self._selection_completed: From 1df7da31466cef6e1db0ff3c497f94139cd69e98 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Fri, 7 Jan 2022 18:42:04 +0000 Subject: [PATCH 2/2] Use clear() instead of removing widget manually --- lib/matplotlib/widgets.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index 73822ac0d8a9..577c5db779df 100644 --- a/lib/matplotlib/widgets.py +++ b/lib/matplotlib/widgets.py @@ -3040,12 +3040,10 @@ def _release(self, event): self._eventrelease.x, self._eventrelease.y = xy1 if self._drawtype != 'none' and self._selector_too_small(): - for artist in self.artists: - artist.set_visible(False) if self._selection_completed: # Call onselect, only when the selection is already existing self.onselect(self._eventpress, self._eventrelease) - self._selection_completed = False + self.clear() else: self.onselect(self._eventpress, self._eventrelease) self._selection_completed = True