Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f99b6ee

Browse files
authored
Merge pull request #25558 from anntzer/ic
Simplify outdated Image.contains check.
2 parents 1a37213 + 6a30406 commit f99b6ee

File tree

1 file changed

+6
-18
lines changed

1 file changed

+6
-18
lines changed

lib/matplotlib/image.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -654,14 +654,9 @@ def draw(self, renderer, *args, **kwargs):
654654

655655
def contains(self, mouseevent):
656656
"""Test whether the mouse event occurred within the image."""
657-
if self._different_canvas(mouseevent):
658-
return False, {}
659-
# 1) This doesn't work for figimage; but figimage also needs a fix
660-
# below (as the check cannot use x/ydata and extents).
661-
# 2) As long as the check below uses x/ydata, we need to test axes
662-
# identity instead of `self.axes.contains(event)` because even if
663-
# axes overlap, x/ydata is only valid for event.inaxes anyways.
664-
if self.axes is not mouseevent.inaxes:
657+
if (self._different_canvas(mouseevent)
658+
# This doesn't work for figimage.
659+
or not self.axes.contains(mouseevent)[0]):
665660
return False, {}
666661
# TODO: make sure this is consistent with patch and patch
667662
# collection on nonlinear transformed coordinates.
@@ -670,16 +665,9 @@ def contains(self, mouseevent):
670665
trans = self.get_transform().inverted()
671666
x, y = trans.transform([mouseevent.x, mouseevent.y])
672667
xmin, xmax, ymin, ymax = self.get_extent()
673-
if xmin > xmax:
674-
xmin, xmax = xmax, xmin
675-
if ymin > ymax:
676-
ymin, ymax = ymax, ymin
677-
678-
if x is not None and y is not None:
679-
inside = (xmin <= x <= xmax) and (ymin <= y <= ymax)
680-
else:
681-
inside = False
682-
668+
# This checks xmin <= x <= xmax *or* xmax <= x <= xmin.
669+
inside = (x is not None and (x - xmin) * (x - xmax) <= 0
670+
and y is not None and (y - ymin) * (y - ymax) <= 0)
683671
return inside, {}
684672

685673
def write_png(self, fname):

0 commit comments

Comments
 (0)