Skip to content
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

FIX: ignore axes that aren't visible #16175

Merged
merged 2 commits into from Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 1 addition & 4 deletions lib/matplotlib/_constrained_layout.py
Expand Up @@ -254,10 +254,7 @@ def _make_ghost_gridspec_slots(fig, gs):
# this gridspec slot doesn't have an axis so we
# make a "ghost".
ax = fig.add_subplot(gs[nn])
ax.set_frame_on(False)
ax.set_xticks([])
ax.set_yticks([])
ax.set_facecolor((1, 0, 0, 0))
ax.set_visible(False)


def _make_layout_margins(ax, renderer, h_pad, w_pad):
Expand Down
7 changes: 3 additions & 4 deletions lib/matplotlib/backend_bases.py
Expand Up @@ -1832,7 +1832,7 @@ def enter_notify_event(self, guiEvent=None, xy=None):

def inaxes(self, xy):
"""
Return the topmost `~.axes.Axes` containing the point *xy*.
Return the topmost visible `~.axes.Axes` containing the point *xy*.

Parameters
----------
Expand All @@ -1842,11 +1842,10 @@ def inaxes(self, xy):
Returns
-------
axes : `~matplotlib.axes.Axes` or None
The topmost axes containing the point, or None if no axes.
The topmost visible axes containing the point, or None if no axes.
"""
axes_list = [a for a in self.figure.get_axes()
if a.patch.contains_point(xy)]

if a.patch.contains_point(xy) and a.get_visible()]
Copy link
Contributor

Choose a reason for hiding this comment

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

perhaps reverse the order of the checks? get_visible() should be faster (just an attribute check) than contains_point()
@jklymak can self-merge with or without this change.

Copy link
Member Author

Choose a reason for hiding this comment

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

The first check is to make sure the pick is actually in the axes (because its in pixels). So if the second check failed, and you didn't have the first, you wouldn't know if it failed because of a resolution change in the tests or because the functionality actually broke.

In normal conditions both tests should pass, so I don't think there is a typical time savings here.

if axes_list:
axes = cbook._topmost_artist(axes_list)
else:
Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Expand Up @@ -6898,3 +6898,11 @@ def test_pi_get_negative_values():
fig, ax = plt.subplots()
with pytest.raises(ValueError):
ax.pie([5, 5, -3], explode=[0, .1, .2])


def test_invisible_axes():
# invisible axes should not respond to events...
fig, ax = plt.subplots()
assert fig.canvas.inaxes((200, 200)) is not None
ax.set_visible(False)
assert fig.canvas.inaxes((200, 200)) is None