From fe924d6166c70a30aea82fd25e52fc9fdb16b78f Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 26 Apr 2019 16:35:29 +0200 Subject: [PATCH] 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() ``` --- lib/matplotlib/image.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 03b651edafb8..fcffc19e6098 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -625,6 +625,13 @@ def contains(self, mouseevent): """ if self._contains is not None: return self._contains(self, mouseevent) + # 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: + return False, {} # TODO: make sure this is consistent with patch and patch # collection on nonlinear transformed coordinates. # TODO: consider returning image coordinates (shouldn't