Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 94397f4

Browse filesBrowse files
committed
BUG: Warn when an existing layout manager changes to tight layout
1 parent 8d2d4c2 commit 94397f4
Copy full SHA for 94397f4

File tree

Expand file treeCollapse file tree

2 files changed

+15
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+15
-0
lines changed

‎lib/matplotlib/figure.py

Copy file name to clipboardExpand all lines: lib/matplotlib/figure.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3430,8 +3430,12 @@ 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 not isinstance(previous_engine, TightLayoutEngine) \
3437+
and previous_engine is not None:
3438+
_api.warn_external('The figure layout has changed to tight')
34353439
finally:
34363440
self.set_layout_engine(None)
34373441

‎lib/matplotlib/tests/test_figure.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_figure.py
+11Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.