From 6a3040634fed83c1cffa378e553409f708f604bd Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 24 Mar 2023 12:04:09 +0100 Subject: [PATCH] Simplify outdated Image.contains check. See discussion at https://github.com/matplotlib/matplotlib/pull/18737/files#r506773948 --- lib/matplotlib/image.py | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index e0589aaef920..055e9510aa76 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -654,14 +654,9 @@ def draw(self, renderer, *args, **kwargs): def contains(self, mouseevent): """Test whether the mouse event occurred within the image.""" - if self._different_canvas(mouseevent): - return False, {} - # 1) This doesn't work for figimage; but figimage also needs a fix - # below (as the check cannot use x/ydata and extents). - # 2) As long as the check below uses x/ydata, we need to test axes - # identity instead of `self.axes.contains(event)` because even if - # axes overlap, x/ydata is only valid for event.inaxes anyways. - if self.axes is not mouseevent.inaxes: + if (self._different_canvas(mouseevent) + # This doesn't work for figimage. + or not self.axes.contains(mouseevent)[0]): return False, {} # TODO: make sure this is consistent with patch and patch # collection on nonlinear transformed coordinates. @@ -670,16 +665,9 @@ def contains(self, mouseevent): trans = self.get_transform().inverted() x, y = trans.transform([mouseevent.x, mouseevent.y]) xmin, xmax, ymin, ymax = self.get_extent() - if xmin > xmax: - xmin, xmax = xmax, xmin - if ymin > ymax: - ymin, ymax = ymax, ymin - - if x is not None and y is not None: - inside = (xmin <= x <= xmax) and (ymin <= y <= ymax) - else: - inside = False - + # This checks xmin <= x <= xmax *or* xmax <= x <= xmin. + inside = (x is not None and (x - xmin) * (x - xmax) <= 0 + and y is not None and (y - ymin) * (y - ymax) <= 0) return inside, {} def write_png(self, fname):