Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 06c96f1

Browse filesBrowse files
committed
Fix contrained layout applying pad multiple times
1 parent 74ae218 commit 06c96f1
Copy full SHA for 06c96f1

File tree

5 files changed

+50
-25
lines changed
Filter options

5 files changed

+50
-25
lines changed

‎lib/matplotlib/_constrained_layout.py

Copy file name to clipboardExpand all lines: lib/matplotlib/_constrained_layout.py
+16-25Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -538,14 +538,10 @@ def match_submerged_margins(layoutgrids, fig):
538538

539539
# interior columns:
540540
if len(ss1.colspan) > 1:
541-
maxsubl = np.max(
542-
lg1.margin_vals['left'][ss1.colspan[1:]] +
543-
lg1.margin_vals['leftcb'][ss1.colspan[1:]]
544-
)
545-
maxsubr = np.max(
546-
lg1.margin_vals['right'][ss1.colspan[:-1]] +
547-
lg1.margin_vals['rightcb'][ss1.colspan[:-1]]
548-
)
541+
leftcb = lg1.margin_vals['leftcb'][ss1.colspan[1:]]
542+
rightcb = lg1.margin_vals['rightcb'][ss1.colspan[:-1]]
543+
maxsubl = np.max(lg1.margin_vals['left'][ss1.colspan[1:]] + leftcb)
544+
maxsubr = np.max(lg1.margin_vals['right'][ss1.colspan[:-1]] + rightcb)
549545
for ax2 in axs:
550546
ss2 = ax2.get_subplotspec()
551547
lg2 = layoutgrids[ss2.get_gridspec()]
@@ -560,22 +556,17 @@ def match_submerged_margins(layoutgrids, fig):
560556
lg2.margin_vals['rightcb'][ss2.colspan[:-1]])
561557
if maxsubr2 > maxsubr:
562558
maxsubr = maxsubr2
563-
for i in ss1.colspan[1:]:
564-
lg1.edit_margin_min('left', maxsubl, cell=i)
565-
for i in ss1.colspan[:-1]:
566-
lg1.edit_margin_min('right', maxsubr, cell=i)
559+
for i, cb in zip(ss1.colspan[1:], leftcb):
560+
lg1.edit_margin_min('left', maxsubl - cb, cell=i)
561+
for i, cb in zip(ss1.colspan[:-1], rightcb):
562+
lg1.edit_margin_min('right', maxsubr - cb, cell=i)
567563

568564
# interior rows:
569565
if len(ss1.rowspan) > 1:
570-
maxsubt = np.max(
571-
lg1.margin_vals['top'][ss1.rowspan[1:]] +
572-
lg1.margin_vals['topcb'][ss1.rowspan[1:]]
573-
)
574-
maxsubb = np.max(
575-
lg1.margin_vals['bottom'][ss1.rowspan[:-1]] +
576-
lg1.margin_vals['bottomcb'][ss1.rowspan[:-1]]
577-
)
578-
566+
topcb = lg1.margin_vals['topcb'][ss1.rowspan[1:]]
567+
bottomcb = lg1.margin_vals['bottomcb'][ss1.rowspan[:-1]]
568+
maxsubt = np.max(lg1.margin_vals['top'][ss1.rowspan[1:]] + topcb)
569+
maxsubb = np.max(lg1.margin_vals['bottom'][ss1.rowspan[:-1]] + bottomcb)
579570
for ax2 in axs:
580571
ss2 = ax2.get_subplotspec()
581572
lg2 = layoutgrids[ss2.get_gridspec()]
@@ -589,10 +580,10 @@ def match_submerged_margins(layoutgrids, fig):
589580
lg2.margin_vals['bottom'][ss2.rowspan[:-1]] +
590581
lg2.margin_vals['bottomcb'][ss2.rowspan[:-1]]
591582
), maxsubb])
592-
for i in ss1.rowspan[1:]:
593-
lg1.edit_margin_min('top', maxsubt, cell=i)
594-
for i in ss1.rowspan[:-1]:
595-
lg1.edit_margin_min('bottom', maxsubb, cell=i)
583+
for i, cb in zip(ss1.rowspan[1:], topcb):
584+
lg1.edit_margin_min('top', maxsubt - cb, cell=i)
585+
for i, cb in zip(ss1.rowspan[:-1], bottomcb):
586+
lg1.edit_margin_min('bottom', maxsubb - cb, cell=i)
596587

597588
return axs
598589

Loading
Loading
Loading

‎lib/matplotlib/tests/test_constrainedlayout.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_constrainedlayout.py
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,3 +741,37 @@ def test_submerged_subfig():
741741
for ax in axs[1:]:
742742
assert np.allclose(ax.get_position().bounds[-1],
743743
axs[0].get_position().bounds[-1], atol=1e-6)
744+
745+
746+
def test_submerged_height_gap():
747+
"""Test that the gap between rows does not depend on the number of columns."""
748+
749+
mosaic1 = "AC;BC"
750+
mosaic2 = "ACDE;BCDE"
751+
752+
fig1, ax_dir1 = plt.subplot_mosaic(mosaic1, layout='constrained')
753+
fig2, ax_dir2 = plt.subplot_mosaic(mosaic2, layout='constrained')
754+
for fig in fig1, fig2:
755+
fig.get_layout_engine().set(h_pad=0.2)
756+
fig.draw_without_rendering()
757+
758+
for label in 'A', 'B':
759+
np.testing.assert_allclose(ax_dir1[label].get_position().bounds[-1],
760+
ax_dir2[label].get_position().bounds[-1])
761+
762+
763+
def test_submerged_width_gap():
764+
"""Test that the gap between columns does not depend on the number of rows."""
765+
766+
mosaic1 = "AB;CC"
767+
mosaic2 = "AB;CC;DD"
768+
769+
fig1, ax_dir1 = plt.subplot_mosaic(mosaic1, layout='constrained')
770+
fig2, ax_dir2 = plt.subplot_mosaic(mosaic2, layout='constrained')
771+
for fig in fig1, fig2:
772+
fig.get_layout_engine().set(w_pad=0.2)
773+
fig.draw_without_rendering()
774+
775+
for label in 'A', 'B':
776+
np.testing.assert_allclose(ax_dir1[label].get_position().bounds[-2],
777+
ax_dir2[label].get_position().bounds[-2])

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.