Skip to content

Commit fe924d6

Browse files
committed
Check axes identity in image.contains.
Otherwise, image.contains(event) will incorrectly return True for a click in *another* axes, somewhere else, that happens to be at the same x/ydata. Try e.g. ``` from pylab import * axs = gcf().subplots(2) im0 = axs[0].imshow([[0, 1], [2, 3]]) im1 = axs[1].imshow([[0, 1], [2, 3]]) gcf().canvas.mpl_connect( "button_press_event", lambda event: print(im0.contains(event), im1.contains(event))) show() ```
1 parent 95f6280 commit fe924d6

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

‎lib/matplotlib/image.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,13 @@ def contains(self, mouseevent):
625625
"""
626626
if self._contains is not None:
627627
return self._contains(self, mouseevent)
628+
# 1) This doesn't work for figimage; but figimage also needs a fix
629+
# below (as the check cannot use x/ydata and extents).
630+
# 2) As long as the check below uses x/ydata, we need to test axes
631+
# identity instead of `self.axes.contains(event)` because even if
632+
# axes overlap, x/ydata is only valid for event.inaxes anyways.
633+
if self.axes is not mouseevent.inaxes:
634+
return False, {}
628635
# TODO: make sure this is consistent with patch and patch
629636
# collection on nonlinear transformed coordinates.
630637
# TODO: consider returning image coordinates (shouldn't

0 commit comments

Comments
 (0)