Skip to content

Commit

Permalink
Backport PR matplotlib#16175: FIX: ignore axes that aren't visible
Browse files Browse the repository at this point in the history
  • Loading branch information
jklymak committed Jan 10, 2020
1 parent 805f451 commit c48fea7
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 9 deletions.
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
9 changes: 4 additions & 5 deletions lib/matplotlib/backend_bases.py
Expand Up @@ -1853,7 +1853,7 @@ def enter_notify_event(self, guiEvent=None, xy=None):

def inaxes(self, xy):
"""
Check if a point is in an axes.
Return the topmost visible `~.axes.Axes` containing the point *xy*.
Parameters
----------
Expand All @@ -1864,12 +1864,11 @@ def inaxes(self, xy):
Returns
-------
axes: topmost axes containing the point, or None if no axes.
axes : `~matplotlib.axes.Axes` or None
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()]
if axes_list:
axes = cbook._topmost_artist(axes_list)
else:
Expand Down
79 changes: 79 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Expand Up @@ -6626,3 +6626,82 @@ def test_aspect_nonlinear_adjustable_datalim():
ax.apply_aspect()
assert ax.get_xlim() == pytest.approx([1*10**(1/2), 100/10**(1/2)])
assert ax.get_ylim() == (1 / 101, 1 / 11)


def test_box_aspect():
# Test if axes with box_aspect=1 has same dimensions
# as axes with aspect equal and adjustable="box"

fig1, ax1 = plt.subplots()
axtwin = ax1.twinx()
axtwin.plot([12, 344])

ax1.set_box_aspect(1)

fig2, ax2 = plt.subplots()
ax2.margins(0)
ax2.plot([0, 2], [6, 8])
ax2.set_aspect("equal", adjustable="box")

fig1.canvas.draw()
fig2.canvas.draw()

bb1 = ax1.get_position()
bbt = axtwin.get_position()
bb2 = ax2.get_position()

assert_array_equal(bb1.extents, bb2.extents)
assert_array_equal(bbt.extents, bb2.extents)


def test_box_aspect_custom_position():
# Test if axes with custom position and box_aspect
# behaves the same independent of the order of setting those.

fig1, ax1 = plt.subplots()
ax1.set_position([0.1, 0.1, 0.9, 0.2])
fig1.canvas.draw()
ax1.set_box_aspect(1.)

fig2, ax2 = plt.subplots()
ax2.set_box_aspect(1.)
fig2.canvas.draw()
ax2.set_position([0.1, 0.1, 0.9, 0.2])

fig1.canvas.draw()
fig2.canvas.draw()

bb1 = ax1.get_position()
bb2 = ax2.get_position()

assert_array_equal(bb1.extents, bb2.extents)


def test_bbox_aspect_axes_init():
# Test that box_aspect can be given to axes init and produces
# all equal square axes.
fig, axs = plt.subplots(2, 3, subplot_kw=dict(box_aspect=1),
constrained_layout=True)
fig.canvas.draw()
renderer = fig.canvas.get_renderer()
sizes = []
for ax in axs.flat:
bb = ax.get_window_extent(renderer)
sizes.extend([bb.width, bb.height])

assert_allclose(sizes, sizes[0])


def test_pi_get_negative_values():
# Test the ValueError raised when feeding negative values into axes.pie
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

0 comments on commit c48fea7

Please sign in to comment.