Skip to content
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
4 changes: 4 additions & 0 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -3430,8 +3430,12 @@ def tight_layout(self, *, pad=1.08, h_pad=None, w_pad=None, rect=None):
engine = TightLayoutEngine(pad=pad, h_pad=h_pad, w_pad=w_pad,
rect=rect)
try:
previous_engine = self.get_layout_engine()
self.set_layout_engine(engine)
engine.execute(self)
if not isinstance(previous_engine, TightLayoutEngine) \
and previous_engine is not None:
_api.warn_external('The figure layout has changed to tight')
finally:
self.set_layout_engine(None)

Expand Down
11 changes: 11 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,17 @@ def test_invalid_layouts():
fig.set_layout_engine("constrained")


@pytest.mark.parametrize('layout', ['constrained', 'compressed'])
def test_layout_change_warning(layout):
"""
Raise a warning when a previously assigned layout changes to tight using
plt.tight_layout().
"""
fig, ax = plt.subplots(layout=layout)
with pytest.warns(UserWarning, match='The figure layout has changed to'):
plt.tight_layout()


@check_figures_equal(extensions=["png", "pdf"])
def test_add_artist(fig_test, fig_ref):
fig_test.dpi = 100
Expand Down