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

Skip to content

Commit 061aad4

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

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/matplotlib/figure.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3430,8 +3430,11 @@ def tight_layout(self, *, pad=1.08, h_pad=None, w_pad=None, rect=None):
34303430
engine = TightLayoutEngine(pad=pad, h_pad=h_pad, w_pad=w_pad,
34313431
rect=rect)
34323432
try:
3433+
previous_engine = self.get_layout_engine()
34333434
self.set_layout_engine(engine)
34343435
engine.execute(self)
3436+
if isinstance(previous_engine, ConstrainedLayoutEngine):
3437+
_api.warn_external('The figure layout has changed to tight')
34353438
finally:
34363439
self.set_layout_engine(None)
34373440

lib/matplotlib/tests/test_figure.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,17 @@ def test_invalid_layouts():
598598
fig.set_layout_engine("constrained")
599599

600600

601+
@pytest.mark.parametrize('layout', ['constrained', 'compressed'])
602+
def test_layout_change_warning(layout):
603+
"""
604+
Raise a warning when a previously assigned layout changes to tight using
605+
plt.tight_layout().
606+
"""
607+
fig, ax = plt.subplots(layout=layout)
608+
with pytest.warns(UserWarning, match='The figure layout has changed to'):
609+
plt.tight_layout()
610+
611+
601612
@check_figures_equal(extensions=["png", "pdf"])
602613
def test_add_artist(fig_test, fig_ref):
603614
fig_test.dpi = 100

0 commit comments

Comments
 (0)