8000 Add inaxes method to FigureCanvas to check whether point is in an axes. by lkjell · Pull Request #9845 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Add inaxes method to FigureCanvas to check whether point is in an axes. #9845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add inaxes method to figure to check whether point is in an axes.
Return the top-most axes if found, else None.
  • Loading branch information
lkjell committed Mar 6, 2018
commit c27e807848bac0ff96d14f6be75e001225b13ece
6 changes: 6 additions & 0 deletions doc/users/next_whats_new/2017-11-24_figure_inaxes.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

Add ``inaxes`` method to Figure
-------------------------------------------------------------

The `Figure` class has now an ``inaxes`` method to check whether a point is in an axes
and returns the topmost axes, else None.
25 changes: 25 additions & 0 deletions 8000 lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,31 @@ def get_axes(self):
"""
return self.axes

def inaxes(self, xy):
"""
Check if a point is in an axes.

Parameters
----------
xy : tuple or list
(x,y) coordinates.
x position - pixels from left of canvas.
y position - pixels from bottom of canvas.

Returns
-------
axes: topmost axes containing the point, or None if no axes.

"""
axes_list = [a for a in self.get_axes() if a.patch.contains_point(xy)]

if axes_list:
axes = max(reversed(axes_list), key=lambda x: x.zorder)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency we probably should use _topmost_artist here (so if we ever need to change that logic we can do it exactly once).

else:
axes = None

return axes

@docstring.dedent_interpd
def legend(self, *args, **kwargs):
"""
Expand Down
0