Skip to content

Commit 5c9f49a

Browse files
authored
Merge pull request #9389 from anntzer/overlayed-axes-events
Assign event to later Axes if zorders are tied.
2 parents 1a5cd54 + 7a8a8e7 commit 5c9f49a

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

lib/matplotlib/backend_bases.py

+14-17
Original file line numberDiff line numberDiff line change
@@ -1537,22 +1537,19 @@ def __init__(self, name, canvas, x, y, guiEvent=None):
15371537
else:
15381538
axes_list = [self.canvas.mouse_grabber]
15391539

1540-
if axes_list: # Use highest zorder.
1541-
self.inaxes = max(axes_list, key=lambda x: x.zorder)
1542-
else: # None found.
1543-
self.inaxes = None
1544-
self._update_enter_leave()
1545-
return
1546-
1547-
try:
1548-
trans = self.inaxes.transData.inverted()
1549-
xdata, ydata = trans.transform_point((x, y))
1550-
except ValueError:
1551-
self.xdata = None
1552-
self.ydata = None
1540+
if axes_list:
1541+
self.inaxes = cbook._topmost_artist(axes_list)
1542+
try:
1543+
trans = self.inaxes.transData.inverted()
1544+
xdata, ydata = trans.transform_point((x, y))
1545+
except ValueError:
1546+
self.xdata = None
1547+
self.ydata = None
1548+
else:
1549+
self.xdata = xdata
1550+
self.ydata = ydata
15531551
else:
1554-
self.xdata = xdata
1555-
self.ydata = ydata
1552+
self.inaxes = None
15561553

15571554
self._update_enter_leave()
15581555

@@ -1815,7 +1812,7 @@ def onRemove(self, ev):
18151812
canvas.mpl_connect('mouse_press_event',canvas.onRemove)
18161813
"""
18171814
# Find the top artist under the cursor
1818-
under = sorted(self.figure.hitlist(ev), key=lambda x: x.zorder)
1815+
under = cbook._topmost_artist(self.figure.hitlist(ev))
18191816
h = None
18201817
if under:
18211818
h = under[-1]
@@ -2899,7 +2896,7 @@ def mouse_move(self, event):
28992896
if a.contains(event) and a.get_visible()]
29002897

29012898
if artists:
2902-
a = max(artists, key=lambda x: x.zorder)
2899+
a = cbook._topmost_artist(artists)
29032900
if a is not event.inaxes.patch:
29042901
data = a.get_cursor_data(event)
29052902
if data is not None:

lib/matplotlib/backend_tools.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def send_message(self, event):
340340
if a.contains(event) and a.get_visible()]
341341

342342
if artists:
343-
a = max(artists, key=lambda x: x.zorder)
343+
a = cbook._topmost_artist(artists)
344344
if a is not event.inaxes.patch:
345345
data = a.get_cursor_data(event)
346346
if data is not None:

lib/matplotlib/cbook/__init__.py

+14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from itertools import repeat
2222
import locale
2323
import numbers
24+
import operator
2425
import os
2526
import re
2627
import sys
@@ -2752,3 +2753,16 @@ def _get_key_params(self):
27522753
(params, str_func))
27532754

27542755
return str_func, params
2756+
2757+
2758+
def _topmost_artist(
2759+
artists,
2760+
_cached_max=functools.partial(max, key=operator.attrgetter("zorder"))):
2761+
"""Get the topmost artist of a list.
2762+
2763+
In case of a tie, return the *last* of the tied artists, as it will be
2764+
drawn on top of the others. `max` returns the first maximum in case of ties
2765+
(on Py2 this is undocumented but true), so we need to iterate over the list
2766+
in reverse order.
2767+
"""
2768+
return _cached_max(reversed(artists))

0 commit comments

Comments
 (0)