Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2fd8f26

Browse files
authored
Merge pull request #9845 from lkjell/inaxes
Add inaxes method to FigureCanvas to check whether point is in an axes.
2 parents 7ff8a66 + cb50c7a commit 2fd8f26

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
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 FigureCanvas
3+
-------------------------------------------------------------
4+
5+
The `FigureCanvas` 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/backend_bases.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,15 +1319,12 @@ def __init__(self, name, canvas, x, y, guiEvent=None):
13191319
self._update_enter_leave()
13201320
return
13211321

1322-
# Find all axes containing the mouse
13231322
if self.canvas.mouse_grabber is None:
1324-
axes_list = [a for a in self.canvas.figure.get_axes()
1325-
if a.in_axes(self)]
1323+
self.inaxes = self.canvas.inaxes((x, y))
13261324
else:
1327-
axes_list = [self.canvas.mouse_grabber]
1325+
self.inaxes = self.canvas.mouse_grabber
13281326

1329-
if axes_list:
1330-
self.inaxes = cbook._topmost_artist(axes_list)
1327+
if self.inaxes is not None:
13311328
try:
13321329
trans = self.inaxes.transData.inverted()
13331330
xdata, ydata = trans.transform_point((x, y))
@@ -1792,6 +1789,39 @@ def enter_notify_event(self, guiEvent=None, xy=None):
17921789
event = LocationEvent('figure_enter_event', self, x, y, guiEvent)
17931790
self.callbacks.process('figure_enter_event', event)
17941791

1792+
@cbook.deprecated("2.1")
1793+
def idle_event(self, guiEvent=None):
1794+
"""Called when GUI is idle."""
1795+
s = 'idle_event'
1796+
event = IdleEvent(s, self, guiEvent=guiEvent)
1797+
self.callbacks.process(s, event)
1798+
1799+
def inaxes(self, xy):
1800+
"""
1801+
Check if a point is in an axes.
1802+
1803+
Parameters
1804+
----------
1805+
xy : tuple or list
1806+
(x,y) coordinates.
1807+
x position - pixels from left of canvas.
1808+
y position - pixels from bottom of canvas.
1809+
1810+
Returns
1811+
-------
1812+
axes: topmost axes containing the point, or None if no axes.
1813+
1814+
"""
1815+
axes_list = [a for a in self.figure.get_axes()
1816+
if a.patch.contains_point(xy)]
1817+
1818+
if axes_list:
1819+
axes = cbook._topmost_artist(axes_list)
1820+
else:
1821+
axes = None
1822+
1823+
return axes
1824+
17951825
def grab_mouse(self, ax):
17961826
"""
17971827
Set the child axes which are currently grabbing the mouse events.

0 commit comments

Comments
 (0)