Skip to content

Commit 85d7295

Browse files
authored
Merge pull request matplotlib#28734 from rcomer/compressed-suptitle
2 parents 3891c15 + 146ba53 commit 85d7295

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
``suptitle`` in compressed layout
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Compressed layout now automatically positions the `~.Figure.suptitle` just
5+
above the top row of axes. To keep this title in its previous position,
6+
either pass ``in_layout=False`` or explicitly set ``y=0.98`` in the
7+
`~.Figure.suptitle` call.

lib/matplotlib/_constrained_layout.py

+7
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ def do_constrained_layout(fig, h_pad, w_pad,
140140
w_pad=w_pad, hspace=hspace, wspace=wspace)
141141
else:
142142
_api.warn_external(warn_collapsed)
143+
144+
if ((suptitle := fig._suptitle) is not None and
145+
suptitle.get_in_layout() and suptitle._autopos):
146+
x, _ = suptitle.get_position()
147+
suptitle.set_position(
148+
(x, layoutgrids[fig].get_inner_bbox().y1 + h_pad))
149+
suptitle.set_verticalalignment('bottom')
143150
else:
144151
_api.warn_external(warn_collapsed)
145152
reset_margins(layoutgrids, fig)

lib/matplotlib/tests/test_constrainedlayout.py

+24
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,30 @@ def test_compressed1():
662662
np.testing.assert_allclose(pos.y0, 0.1934, atol=1e-3)
663663

664664

665+
def test_compressed_suptitle():
666+
fig, (ax0, ax1) = plt.subplots(
667+
nrows=2, figsize=(4, 10), layout="compressed",
668+
gridspec_kw={"height_ratios": (1 / 4, 3 / 4), "hspace": 0})
669+
670+
ax0.axis("equal")
671+
ax0.set_box_aspect(1/3)
672+
673+
ax1.axis("equal")
674+
ax1.set_box_aspect(1)
675+
676+
title = fig.suptitle("Title")
677+
fig.draw_without_rendering()
678+
assert title.get_position()[1] == pytest.approx(0.7457, abs=1e-3)
679+
680+
title = fig.suptitle("Title", y=0.98)
681+
fig.draw_without_rendering()
682+
assert title.get_position()[1] == 0.98
683+
684+
title = fig.suptitle("Title", in_layout=False)
685+
fig.draw_without_rendering()
686+
assert title.get_position()[1] == 0.98
687+
688+
665689
@pytest.mark.parametrize('arg, state', [
666690
(True, True),
667691
(False, False),

0 commit comments

Comments
 (0)