Skip to content

Commit aa4af14

Browse files
authored
Merge pull request #24528 from j1642/warn-if-tight-layout
BUG: Warn when an existing layout manager changes to tight layout
2 parents 80cab2d + 94397f4 commit aa4af14

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/matplotlib/figure.py

+4
Original file line numberDiff line numberDiff line change
@@ -3434,8 +3434,12 @@ def tight_layout(self, *, pad=1.08, h_pad=None, w_pad=None, rect=None):
34343434
engine = TightLayoutEngine(pad=pad, h_pad=h_pad, w_pad=w_pad,
34353435
rect=rect)
34363436
try:
3437+
previous_engine = self.get_layout_engine()
34373438
self.set_layout_engine(engine)
34383439
engine.execute(self)
3440+
if not isinstance(previous_engine, TightLayoutEngine) \
3441+
and previous_engine is not None:
3442+
_api.warn_external('The figure layout has changed to tight')
34393443
finally:
34403444
self.set_layout_engine(None)
34413445

lib/matplotlib/tests/test_figure.py

+11
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,17 @@ def test_invalid_layouts():
603603
fig.set_layout_engine("constrained")
604604

605605

606+
@pytest.mark.parametrize('layout', ['constrained', 'compressed'])
607+
def test_layout_change_warning(layout):
608+
"""
609+
Raise a warning when a previously assigned layout changes to tight using
610+
plt.tight_layout().
611+
"""
612+
fig, ax = plt.subplots(layout=layout)
613+
with pytest.warns(UserWarning, match='The figure layout has changed to'):
614+
plt.tight_layout()
615+
616+
606617
@check_figures_equal(extensions=["png", "pdf"])
607618
def test_add_artist(fig_test, fig_ref):
608619
fig_test.dpi = 100

0 commit comments

Comments
 (0)