Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1133d85

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

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/matplotlib/figure.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2506,6 +2506,10 @@ def _check_layout_engines_compat(self, old, new):
25062506
"""
25072507
if old is None or new is None:
25082508
return True
2509+
if isinstance(old, ConstrainedLayoutEngine) \
2510+
and isinstance(new, TightLayoutEngine):
2511+
_api.warn_external("The layout manager has been changed to "
2512+
"'tight'.")
25092513
if old.colorbar_gridspec == new.colorbar_gridspec:
25102514
return True
25112515
# colorbar layout different, so check if any colorbars are on the

lib/matplotlib/tests/test_figure.py

Lines changed: 12 additions & 2 deletions
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)
@@ -581,7 +582,8 @@ def test_invalid_layouts():
581582
pc = ax.pcolormesh(np.random.randn(2, 2))
582583
fig.colorbar(pc)
583584
with pytest.raises(RuntimeError, match='Colorbar layout of new layout'):
584-
fig.set_layout_engine("tight")
585+
with pytest.warns(UserWarning):
586+
fig.set_layout_engine("tight")
585587
fig.set_layout_engine("none")
586588
with pytest.raises(RuntimeError, match='Colorbar layout of new layout'):
587589
fig.set_layout_engine("tight")
@@ -598,6 +600,14 @@ def test_invalid_layouts():
598600
fig.set_layout_engine("constrained")
599601

600602

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

0 commit comments

Comments
 (0)