8000 Simplify outdated Image.contains check. by anntzer · Pull Request #25558 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Simplify outdated Image.contains check. #25558

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
Jul 16, 2023
Merged
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
24 changes: 6 additions & 18 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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):
Expand Down
0