8000 Backport PR #14919: FIX constrained_layout w/ hidden axes · matplotlib/matplotlib@ab203bd · GitHub
[go: up one dir, main page]

Skip to content

Commit ab203bd

Browse files
anntzerMeeseeksDev[bot]
authored and
MeeseeksDev[bot]
committed
Backport PR #14919: FIX constrained_layout w/ hidden axes
1 parent 6665d1f commit ab203bd

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/matplotlib/_constrained_layout.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,11 @@ def _make_layout_margins(ax, renderer, h_pad, w_pad):
280280
invTransFig = fig.transFigure.inverted().transform_bbox
281281
pos = ax.get_position(original=True)
282282
tightbbox = ax.get_tightbbox(renderer=renderer)
283-
bbox = invTransFig(tightbbox)
283+
if tightbbox is None:
284+
bbox = pos
285+
else:
286+
bbox = invTransFig(tightbbox)
287+
284288
# this can go wrong:
285289
if not (np.isfinite(bbox.width) and np.isfinite(bbox.height)):
286290
# just abort, this is likely a bad set of co-ordinates that

lib/matplotlib/tests/test_constrainedlayout.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,16 @@ def test_colorbar_location():
404404
fig.colorbar(pcm, ax=axs[-2, 3:], shrink=0.5, location='top')
405405
fig.colorbar(pcm, ax=axs[0, 0], shrink=0.5, location='left')
406406
fig.colorbar(pcm, ax=axs[1:3, 2], shrink=0.5, location='right')
407+
408+
409+
def test_hidden_axes():
410+
# test that if we make an axes not visible that constrained_layout
411+
# still works. Note the axes still takes space in the layout
412+
# (as does a gridspec slot that is empty)
413+
fig, axs = plt.subplots(2, 2, constrained_layout=True)
414+
axs[0, 1].set_visible(False)
415+
fig.canvas.draw()
416+
extents1 = np.copy(axs[0, 0].get_position().extents)
417+
418+
np.testing.assert_allclose(extents1,
419+
[0.045552, 0.548288, 0.47319, 0.982638], rtol=1e-5)

0 commit comments

Comments
 (0)
0