Skip to content

Commit 7063e3f

Browse files
committed
ENH: implement and use base layout_engine for more flexible layout.
1 parent b70364f commit 7063e3f

File tree

13 files changed

+587
-195
lines changed

13 files changed

+587
-195
lines changed

doc/api/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Alphabetical list of modules:
6666
fontconfig_pattern_api.rst
6767
gridspec_api.rst
6868
image_api.rst
69+
layout_engine_api.rst
6970
legend_api.rst
7071
legend_handler_api.rst
7172
lines_api.rst

doc/api/layout_engine_api.rst

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
****************************
2+
``matplotlib.layout_engine``
3+
****************************
4+
5+
.. currentmodule:: matplotlib.layout_engine
6+
7+
.. automodule:: matplotlib.layout_engine
8+
:members:
9+
:inherited-members:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Incompatible layout engines raise
2+
---------------------------------
3+
``tight_layout`` and ``constrained_layout`` are incompatible if
4+
a colorbar has been added to the figure. Invoking the incompatible layout
5+
engine used to warn, but now raises with a ``RuntimeError``.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
New ``layout_engine`` module
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Matplotlib ships with ``tight_layout`` and ``constrained_layout`` layout
5+
engines. A new ``layout_engine`` module is provided to allow downstream
6+
libraries to write their own layout engines and `~.figure.Figure` objects can
7+
now take a `.LayoutEngine` subclass as an argument to the *layout* parameter.

lib/matplotlib/_constrained_layout.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import numpy as np
1919

2020
from matplotlib import _api, artist as martist
21+
from matplotlib.backend_bases import _get_renderer
2122
import matplotlib.transforms as mtransforms
2223
import matplotlib._layoutgrid as mlayoutgrid
2324

@@ -62,7 +63,7 @@
6263

6364

6465
######################################################
65-
def do_constrained_layout(fig, renderer, h_pad, w_pad,
66+
def do_constrained_layout(fig, h_pad, w_pad,
6667
hspace=None, wspace=None):
6768
"""
6869
Do the constrained_layout. Called at draw time in
@@ -91,6 +92,7 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
9192
layoutgrid : private debugging structure
9293
"""
9394

95+
renderer = _get_renderer(fig)
9496
# make layoutgrid tree...
9597
layoutgrids = make_layoutgrids(fig, None)
9698
if not layoutgrids['hasgrids']:

lib/matplotlib/_tight_bbox.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ def adjust_bbox(fig, bbox_inches, fixed_dpi=None):
1717
"""
1818
origBbox = fig.bbox
1919
origBboxInches = fig.bbox_inches
20-
orig_tight_layout = fig.get_tight_layout()
20+
orig_layout = fig.get_layout_engine()
21+
fig.set_layout_engine(None)
2122
_boxout = fig.transFigure._boxout
2223

23-
fig.set_tight_layout(False)
24-
2524
old_aspect = []
2625
locator_list = []
2726
sentinel = object()
@@ -47,7 +46,7 @@ def restore_bbox():
4746

4847
fig.bbox = origBbox
4948
fig.bbox_inches = origBboxInches
50-
fig.set_tight_layout(orig_tight_layout)
49+
fig.set_layout_engine(orig_layout)
5150
fig.transFigure._boxout = _boxout
5251
fig.transFigure.invalidate()
5352
fig.patch.set_bounds(0, 0, 1, 1)

lib/matplotlib/backend_bases.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2226,7 +2226,7 @@ def print_figure(
22262226
if bbox_inches is None:
22272227
bbox_inches = rcParams['savefig.bbox']
22282228

2229-
if (self.figure.get_constrained_layout() or
2229+
if (self.figure.get_layout_engine() is not None or
22302230
bbox_inches == "tight"):
22312231
# we need to trigger a draw before printing to make sure
22322232
# CL works. "tight" also needs a draw to get the right
@@ -2255,8 +2255,8 @@ def print_figure(
22552255
else:
22562256
_bbox_inches_restore = None
22572257

2258-
# we have already done CL above, so turn it off:
2259-
stack.enter_context(self.figure._cm_set(constrained_layout=False))
2258+
# we have already done layout above, so turn it off:
2259+
stack.enter_context(self.figure._cm_set(layout_engine=None))
22602260
try:
22612261
# _get_renderer may change the figure dpi (as vector formats
22622262
# force the figure dpi to 72), so we need to set it again here.

0 commit comments

Comments
 (0)