8000 BUG: Warn when an existing layout manager changes to tight layout · matplotlib/matplotlib@4bbd822 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4bbd822

Browse files
committed
BUG: Warn when an existing layout manager changes to tight layout
1 parent 8d2d4c2 commit 4bbd822

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/matplotlib/figure.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2576,6 +2576,10 @@ def set_layout_engine(self, layout=None, **kwargs):
25762576

25772577
if self._check_layout_engines_compat(self._layout_engine,
25782578
new_layout_engine):
2579+
if isinstance(self._layout_engine, ConstrainedLayoutEngine) \
2580+
and isinstance(new_layout_engine, TightLayoutEngine):
2581+
_api.warn_external("The layout manager has been changed to "
2582+
"'tight'.")
25792583
self._layout_engine = new_layout_engine
25802584
else:
25812585
raise RuntimeError('Colorbar layout of new layout engine not '

lib/matplotlib/tests/test_figure.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,8 @@ def test_invalid_layouts():
571571

572572
# test that layouts can be swapped if no colorbar:
573573
fig, ax = plt.subplots(layout="constrained")
574-
fig.set_layout_engine("tight")
574+
with pytest.warns(UserWarning):
575+
fig.set_layout_engine("tight")
575576
assert isinstance(fig.get_layout_engine(), TightLayoutEngine)
576577
fig.set_layout_engine("constrained")
577578
assert isinstance(fig.get_layout_engine(), ConstrainedLayoutEngine)
@@ -598,6 +599,14 @@ def test_invalid_layouts():
598599
fig.set_layout_engine("constrained")
599600

600601

602+
@pytest.mark.parametrize('layout', ['constrained', 'compressed'])
603+
def test_layout_change_warning(layout):
604+
"""Raise a warning when an existing layout manager is changed to tight."""
605+
fig, ax = plt.subplots(layout=layout)
606+
with pytest.warns(UserWarning, match='The layout manager has been'):
607+
plt.tight_layout()
608+
609+
601610
@check_figures_equal(extensions=["png", "pdf"])
602611
def test_add_artist(fig_test, fig_ref):
603612
fig_test.dpi = 100

0 commit comments

Comments
 (0)
0