Skip to content

Commit dd7c2e0

Browse files
committed
ENH: add a NullLayoutEngine
It can mirror what ever was there to make sure things stay compatible.
1 parent bf8a992 commit dd7c2e0

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

lib/matplotlib/figure.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@
3535

3636
from matplotlib.axes import Axes, SubplotBase, subplot_class_factory
3737
from matplotlib.gridspec import GridSpec
38-
from matplotlib.layout_engine import (ConstrainedLayoutEngine,
39-
TightLayoutEngine, LayoutEngine)
38+
from matplotlib.layout_engine import (
39+
ConstrainedLayoutEngine,TightLayoutEngine, LayoutEngine, NullLayoutEnigne
40+
)
4041
import matplotlib.legend as mlegend
4142
from matplotlib.patches import Rectangle
4243
from matplotlib.text import Text
@@ -2356,7 +2357,13 @@ def set_layout_engine(self, layout=None, **kwargs):
23562357
elif layout == 'constrained':
23572358
new_layout_engine = ConstrainedLayoutEngine(**kwargs)
23582359
elif layout == 'none':
2359-
new_layout_engine = None
2360+
if self._layout_engine is not None:
2361+
new_layout_engine = NullLayoutEnigne(
2362+
self._layout_engine.adjust_compatible,
2363+
self._layout_engine.colorbar_gridspec
2364+
)
2365+
else:
2366+
new_layout_engine = None
23602367
elif isinstance(layout, LayoutEngine):
23612368
new_layout_engine = layout
23622369
else:

lib/matplotlib/layout_engine.py

+22
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,28 @@ def execute(self, fig):
101101
raise NotImplementedError
102102

103103

104+
class NullLayoutEnigne(LayoutEngine):
105+
"""
106+
A no-op LayoutEngine.
107+
108+
This is primarily to act as a place holder if we remove a layout engine.
109+
110+
Parameter
111+
---------
112+
adjust_compatible, colorbar_gridspec : bool
113+
Allow the NullLayoutEnigne to mirror the behavior of whatever
114+
layout engine it is replacing.
115+
116+
"""
117+
def __init__(self, adjust_compatible, colorbar_gridspec, **kwargs):
118+
self._adjust_compatible = adjust_compatible
119+
self._colorbar_gridspec = colorbar_gridspec
120+
super().__init__(**kwargs)
121+
122+
def execute(self, fig):
123+
return
124+
125+
104126
class TightLayoutEngine(LayoutEngine):
105127
"""
106128
Implements the ``tight_layout`` geometry management. See

lib/matplotlib/tests/test_figure.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
from matplotlib.axes import Axes
1919
from matplotlib.figure import Figure
2020
from matplotlib.layout_engine import (ConstrainedLayoutEngine,
21-
TightLayoutEngine)
21+
TightLayoutEngine,
22+
NullLayoutEnigne)
2223
from matplotlib.ticker import AutoMinorLocator, FixedFormatter, ScalarFormatter
2324
import matplotlib.pyplot as plt
2425
import matplotlib.dates as mdates
@@ -610,17 +611,23 @@ def test_invalid_layouts():
610611
fig, ax = plt.subplots(layout="constrained")
611612
pc = ax.pcolormesh(np.random.randn(2, 2))
612613
fig.colorbar(pc)
614+
with pytest.raises(RuntimeError, match='Colorbar layout of new layout'):
615+
fig.set_layout_engine("tight")
616+
fig.set_layout_engine("none")
613617
with pytest.raises(RuntimeError, match='Colorbar layout of new layout'):
614618
fig.set_layout_engine("tight")
615619

616620
fig, ax = plt.subplots(layout="tight")
617621
pc = ax.pcolormesh(np.random.randn(2, 2))
618622
fig.colorbar(pc)
623+
with pytest.raises(RuntimeError, match='Colorbar layout of new layout'):
624+
fig.set_layout_engine("constrained")
625+
fig.set_layout_engine("none")
619626
with pytest.raises(RuntimeError, match='Colorbar layout of new layout'):
620627
fig.set_layout_engine("constrained")
621628

622629
fig.set_layout_engine("none")
623-
assert fig.get_layout_engine() is None
630+
assert isinstance(fig.get_layout_engine(), NullLayoutEnigne)
624631

625632

626633
@check_figures_equal(extensions=["png", "pdf"])

0 commit comments

Comments
 (0)