Skip to content

Compressed layout moves suptitle #28734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/api/next_api_changes/behavior/28734-REC.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
``suptitle`` in compressed layout
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Compressed layout now automatically positions the `~.Figure.suptitle` just
above the top row of axes. To keep this title in its previous position,
either pass ``in_layout=False`` or explicitly set ``y=0.98`` in the
`~.Figure.suptitle` call.
7 changes: 7 additions & 0 deletions lib/matplotlib/_constrained_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ def do_constrained_layout(fig, h_pad, w_pad,
w_pad=w_pad, hspace=hspace, wspace=wspace)
else:
_api.warn_external(warn_collapsed)

if ((suptitle := fig._suptitle) is not None and
suptitle.get_in_layout() and suptitle._autopos):
x, _ = suptitle.get_position()
suptitle.set_position(
(x, layoutgrids[fig].get_inner_bbox().y1 + h_pad))
suptitle.set_verticalalignment('bottom')
else:
_api.warn_external(warn_collapsed)
reset_margins(layoutgrids, fig)
Expand Down
24 changes: 24 additions & 0 deletions lib/matplotlib/tests/test_constrainedlayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,30 @@ def test_compressed1():
np.testing.assert_allclose(pos.y0, 0.1934, atol=1e-3)


def test_compressed_suptitle():
fig, (ax0, ax1) = plt.subplots(
nrows=2, figsize=(4, 10), layout="compressed",
gridspec_kw={"height_ratios": (1 / 4, 3 / 4), "hspace": 0})

ax0.axis("equal")
ax0.set_box_aspect(1/3)

ax1.axis("equal")
ax1.set_box_aspect(1)

title = fig.suptitle("Title")
fig.draw_without_rendering()
assert title.get_position()[1] == pytest.approx(0.7457, abs=1e-3)

title = fig.suptitle("Title", y=0.98)
fig.draw_without_rendering()
assert title.get_position()[1] == 0.98

title = fig.suptitle("Title", in_layout=False)
fig.draw_without_rendering()
assert title.get_position()[1] == 0.98


@pytest.mark.parametrize('arg, state', [
(True, True),
(False, False),
Expand Down
Loading