Skip to content

Commit e6442fc

Browse files
committed
Add Figure parameter layout and discourage tight_layout / constrained_layout
1 parent 17a7bb1 commit e6442fc

File tree

2 files changed

+68
-12
lines changed

2 files changed

+68
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Discouraged: ``Figure`` parameters ``tight_layout`` and ``constrained_layout``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
The ``Figure`` parameters ``tight_layout`` and ``constrained_layout`` are
4+
triggering competing layout mechanisms and thus should not be used together.
5+
6+
To make the API clearer, we've merged them under the new parameter ``layout``
7+
with values 'constrained' (equal to ``constrained_layout=True``), 'tight'
8+
(equal to ``tight_layout=True``) and a dict (equal to calling ``tight_layout``
9+
with that dict). If given ``layout`` takes precedence.
10+
11+
The use of ``tight_layout`` and ``constrained_layout`` is discouraged in
12+
favor of ``layout``. However, these parameters will stay available for
13+
backward compatibility.

lib/matplotlib/figure.py

+55-12
Original file line numberDiff line numberDiff line change
@@ -2123,6 +2123,8 @@ def __init__(self,
21232123
subplotpars=None, # rc figure.subplot.*
21242124
tight_layout=None, # rc figure.autolayout
21252125
constrained_layout=None, # rc figure.constrained_layout.use
2126+
*,
2127+
layout=None,
21262128
):
21272129
"""
21282130
Parameters
@@ -2151,22 +2153,63 @@ def __init__(self,
21512153
parameters :rc:`figure.subplot.*` are used.
21522154
21532155
tight_layout : bool or dict, default: :rc:`figure.autolayout`
2154-
If ``False`` use *subplotpars*. If ``True`` adjust subplot
2155-
parameters using `.tight_layout` with default padding.
2156-
When providing a dict containing the keys ``pad``, ``w_pad``,
2157-
``h_pad``, and ``rect``, the default `.tight_layout` paddings
2158-
will be overridden.
2156+
This is equal to ``layout='tight'`` or ``layout=dict(...)``.
2157+
2158+
.. admonition:: Discouraged
2159+
2160+
The use of this parameter is discouraged. Please use
2161+
``layout='tight'`` or ``layout=dict(...)`` instead.
21592162
21602163
constrained_layout : bool, default: :rc:`figure.constrained_layout.use`
2161-
If ``True`` use constrained layout to adjust positioning of plot
2162-
elements. Like ``tight_layout``, but designed to be more
2163-
flexible. See
2164-
:doc:`/tutorials/intermediate/constrainedlayout_guide`
2165-
for examples. (Note: does not work with `add_subplot` or
2166-
`~.pyplot.subplot2grid`.)
2164+
This is equal to ``layout='constrained'``.
2165+
2166+
.. admonition:: Discouraged
2167+
2168+
The use of this parameter is discouraged. Please use
2169+
``layout='constrained'`` instead.
2170+
2171+
layout : {'constrained', 'tight'}, optional
2172+
The layout mechanism for positioning of plot elements.
2173+
Supported values:
2174+
2175+
- 'constrained': The constrained layout solver usually gives the
2176+
best layout results and is thus recommended. However, it is
2177+
computationally expensive and can be slow for complex figures
2178+
with many elements.
2179+
2180+
See :doc:`/tutorials/intermediate/constrainedlayout_guide`
2181+
for examples. (Note: does not work with `add_subplot` or
2182+
`~.pyplot.subplot2grid`.)
2183+
2184+
- 'tight': Use the tight layout mechanism. This is a relatively
2185+
simple algorithm, that adjusts the subplot parameters so that
2186+
decorations like tick labels, axis labels and titles have enough
2187+
space. See `.Figure.set_tight_layout` for further details.
2188+
2189+
If not given, fall back to using the parameters *tight_layout* and
2190+
*constrained_layout*, including their config defaults
2191+
:rc:`figure.autolayout` and :rc:`figure.constrained_layout.use`.
21672192
"""
21682193
super().__init__()
21692194

2195+
if layout is not None:
2196+
if tight_layout is not None:
2197+
_api.warn_external(
2198+
"The Figure parameters 'layout' and 'tight_layout' "
2199+
"cannot be used together. Please use 'layout' only.")
2200+
if constrained_layout is not None:
2201+
_api.warn_external(
2202+
"The Figure parameters 'layout' and 'constrained_layout' "
2203+
"cannot be used together. Please use 'layout' only.")
2204+
if layout == 'constrained':
2205+
tight_layout = False
2206+
constrained_layout = True
2207+
elif layout == 'tight':
2208+
tight_layout = True
2209+
constrained_layout = False
2210+
else:
2211+
raise ValueError(f"Invalid value for 'layout': {layout!r}")
2212+
21702213
self.callbacks = cbook.CallbackRegistry()
21712214
# Callbacks traditionally associated with the canvas (and exposed with
21722215
# a proxy property), but that actually need to be on the figure for
@@ -2330,7 +2373,7 @@ def set_tight_layout(self, tight):
23302373
----------
23312374
tight : bool or dict with keys "pad", "w_pad", "h_pad", "rect" or None
23322375
If a bool, sets whether to call `.tight_layout` upon drawing.
2333-
If ``None``, use the ``figure.autolayout`` rcparam instead.
2376+
If ``None``, use :rc:`figure.autolayout` instead.
23342377
If a dict, pass it as kwargs to `.tight_layout`, overriding the
23352378
default paddings.
23362379
"""

0 commit comments

Comments
 (0)