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

Skip to content

Commit 18e19b3

Browse files
committed
FIX: do not warn when calling tight_layout multiple times
Closes #26290
1 parent 02d2e13 commit 18e19b3

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

lib/matplotlib/figure.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
from matplotlib.gridspec import GridSpec
5454
from matplotlib.layout_engine import (
5555
ConstrainedLayoutEngine, TightLayoutEngine, LayoutEngine,
56-
PlaceHolderLayoutEngine
56+
PlaceHolderLayoutEngine, TLPlaceHolderLayoutEngine
5757
)
5858
import matplotlib.legend as mlegend
5959
from matplotlib.patches import Rectangle
@@ -3522,17 +3522,25 @@ def tight_layout(self, *, pad=1.08, h_pad=None, w_pad=None, rect=None):
35223522
# note that here we do not permanently set the figures engine to
35233523
# tight_layout but rather just perform the layout in place and remove
35243524
# any previous engines.
3525-
engine = TightLayoutEngine(pad=pad, h_pad=h_pad, w_pad=w_pad,
3526-
rect=rect)
3525+
engine = TightLayoutEngine(pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
35273526
try:
35283527
previous_engine = self.get_layout_engine()
35293528
self.set_layout_engine(engine)
35303529
engine.execute(self)
3531-
if not isinstance(previous_engine, TightLayoutEngine) \
3532-
and previous_engine is not None:
3533-
_api.warn_external('The figure layout has changed to tight')
3530+
if previous_engine is not None and not isinstance(
3531+
previous_engine, (TightLayoutEngine, TLPlaceHolderLayoutEngine)
3532+
):
3533+
_api.warn_external("The figure layout has changed to tight")
35343534
finally:
3535-
self.set_layout_engine('none')
3535+
if previous_engine is not None:
3536+
self.set_layout_engine(
3537+
TLPlaceHolderLayoutEngine(
3538+
previous_engine.adjust_compatible,
3539+
previous_engine.colorbar_gridspec
3540+
)
3541+
)
3542+
else:
3543+
self.set_layout_engine(TLPlaceHolderLayoutEngine(True, True))
35363544

35373545

35383546
def figaspect(arg):

lib/matplotlib/layout_engine.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ def execute(self, fig):
129129
return
130130

131131

132+
class TLPlaceHolderLayoutEngine(PlaceHolderLayoutEngine):
133+
"""Special place holder class used by Figure.tight_layout"""
134+
...
135+
136+
132137
class TightLayoutEngine(LayoutEngine):
133138
"""
134139
Implements the ``tight_layout`` geometry management. See

lib/matplotlib/layout_engine.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class PlaceHolderLayoutEngine(LayoutEngine):
1818
) -> None: ...
1919
def execute(self, fig: Figure) -> None: ...
2020

21+
class TLPlaceHolderLayoutEngine(PlaceHolderLayoutEngine):
22+
...
23+
2124
class TightLayoutEngine(LayoutEngine):
2225
def __init__(
2326
self,

lib/matplotlib/tests/test_figure.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,14 @@ def test_layout_change_warning(layout):
701701
plt.tight_layout()
702702

703703

704+
def test_repeated_tightlayout():
705+
fig = Figure()
706+
fig.tight_layout()
707+
# subsequent calls should not warn
708+
fig.tight_layout()
709+
fig.tight_layout()
710+
711+
704712
@check_figures_equal(extensions=["png", "pdf"])
705713
def test_add_artist(fig_test, fig_ref):
706714
fig_test.dpi = 100

0 commit comments

Comments
 (0)