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

Skip to content

Commit 27fd29b

Browse files
committed
FIX: Better constrain CL for uneven grids
1 parent 28f41f9 commit 27fd29b

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lib/matplotlib/_constrained_layout.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,25 @@ def _align_spines(fig, gs):
403403
if height0 > height1:
404404
ax0._poslayoutbox.constrain_height_min(
405405
ax1._poslayoutbox.height * height0 / height1)
406+
# the precise constraint must be weak because
407+
# we may have one col with two axes, and the other
408+
# column with just one, and we want the top and
409+
# bottom of the short axes to align with the large one,
410+
# so the height of the long one is greater than
411+
# twice the height of the small ones. OTOH, if the
412+
# axes are not so constrained, we would like the pos
413+
# boxes to be the proper relative widths.
414+
# See test_constrained_layout17 fo
415+
ax0._poslayoutbox.constrain_height(
416+
ax1._poslayoutbox.height * height0 / height1,
417+
strength='weak')
406418
elif height0 < height1:
407419
ax1._poslayoutbox.constrain_height_min(
408420
ax0._poslayoutbox.height * height1 / height0)
421+
# see above
422+
ax1._poslayoutbox.constrain_height(
423+
ax0._poslayoutbox.height * height1 / height0,
424+
strength='weak')
409425
# For widths, do it if the subplots share a row.
410426
if not alignwidth and len(colspan0) == len(colspan1):
411427
ax0._poslayoutbox.constrain_width(
@@ -415,9 +431,17 @@ def _align_spines(fig, gs):
415431
if width0 > width1:
416432
ax0._poslayoutbox.constrain_width_min(
417433
ax1._poslayoutbox.width * width0 / width1)
434+
# see comment above
435+
ax0._poslayoutbox.constrain_width(
436+
ax1._poslayoutbox.width * width0 / width1,
437+
strength='weak')
418438
elif width0 < width1:
419439
ax1._poslayoutbox.constrain_width_min(
420440
ax0._poslayoutbox.width * width1 / width0)
441+
# see comment above
442+
ax1._poslayoutbox.constrain_width_min(
443+
ax0._poslayoutbox.width * width1 / width0,
444+
strength='weak')
421445

422446

423447
def _arrange_subplotspecs(gs, hspace=0, wspace=0):

lib/matplotlib/tests/test_constrainedlayout.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,20 @@ def test_hidden_axes():
399399

400400
np.testing.assert_allclose(
401401
extents1, [0.045552, 0.548288, 0.47319, 0.982638], rtol=1e-5)
402+
403+
404+
def test_two_under_one():
405+
fig = plt.figure(constrained_layout=True)
406+
gs = fig.add_gridspec(2, 2)
407+
ax0 = fig.add_subplot(gs[0, :])
408+
ax10 = fig.add_subplot(gs[1, 0])
409+
ax11 = fig.add_subplot(gs[1, 1])
410+
fig.canvas.draw()
411+
extents0 = np.copy(ax0.get_position().extents)
412+
extents10 = np.copy(ax10.get_position().extents)
413+
extents11 = np.copy(ax11.get_position().extents)
414+
415+
np.testing.assert_allclose(extents0[0], extents10[0])
416+
np.testing.assert_allclose(extents0[2], extents11[2])
417+
np.testing.assert_allclose(extents11[0] - extents10[2],
418+
0.077362, atol=1e-6)

0 commit comments

Comments
 (0)