8000 Add inaxes method to figure to check whether point is in an axes. · matplotlib/matplotlib@c27e807 · GitHub
[go: up one dir, main page]

Skip to content

Commit c27e807

Browse files
committed
Add inaxes method to figure to check whether point is in an axes.
Return the top-most axes if found, else None.
1 parent 5e22976 commit c27e807

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
Add ``inaxes`` method to Figure
3+
-------------------------------------------------------------
4+
5+
The `Figure` class has now an ``inaxes`` method to check whether a point is in an axes
6+
and returns the topmost axes, else None.

lib/matplotlib/figure.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,31 @@ def get_axes(self):
15321532
"""
15331533
return self.axes
15341534

1535+
def inaxes(self, xy):
1536+
"""
1537+
Check if a point is in an axes.
1538+
1539+
Parameters
1540+
----------
1541+
xy : tuple or list
1542+
(x,y) coordinates.
1543+
x position - pixels from left of canvas.
1544+
y position - pixels from bottom of canvas.
1545+
1546+
Returns
1547+
-------
1548+
axes: topmost axes containing the point, or None if no axes.
1549+
1550+
"""
1551+
axes_list = [a for a in self.get_axes() if a.patch.contains_point(xy)]
1552+
1553+
if axes_list:
1554+
axes = max(reversed(axes_list), key=lambda x: x.zorder)
1555+
else:
1556+
axes = None
1557+
1558+
return axes
1559+
15351560
@docstring.dedent_interpd
15361561
def legend(self, *args, **kwargs):
15371562
"""

0 commit comments

Comments
 (0)
0