Skip to content

Commit d001ed1

Browse files
timhoffmjklymak
authored andcommitted
Add Figure parameter layout and discourage tight_layout / constrained_layout
1 parent 0b8481d commit d001ed1

File tree

2 files changed

+67
-12
lines changed

2 files changed

+67
-12
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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``). If given ``layout`` takes precedence.
9+
10+
The use of ``tight_layout`` and ``constrained_layout`` is discouraged in
11+
favor of ``layout``. However, these parameters will stay available for
12+
backward compatibility.

lib/matplotlib/figure.py

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,6 +2138,8 @@ def __init__(self,
21382138
subplotpars=None, # rc figure.subplot.*
21392139
tight_layout=None, # rc figure.autolayout
21402140
constrained_layout=None, # rc figure.constrained_layout.use
2141+
*,
2142+
layout=None,
21412143
):
21422144
"""
21432145
Parameters
@@ -2166,22 +2168,63 @@ def __init__(self,
21662168
parameters :rc:`figure.subplot.*` are used.
21672169
21682170
tight_layout : bool or dict, default: :rc:`figure.autolayout`
2169-
If ``False`` use *subplotpars*. If ``True`` adjust subplot
2170-
parameters using `.tight_layout` with default padding.
2171-
When providing a dict containing the keys ``pad``, ``w_pad``,
2172-
``h_pad``, and ``rect``, the default `.tight_layout` paddings
2173-
will be overridden.
2171+
Whether to use the tight layout mechanism. See `.set_tight_layout`.
2172+
2173+
.. admonition:: Discouraged
2174+
2175+
The use of this parameter is discouraged. Please use
2176+
``layout='tight'`` instead for the common case
2177+
``tight_layout=True`` and use `.set_tight_layout` otherwise.
21742178
21752179
constrained_layout : bool, default: :rc:`figure.constrained_layout.use`
2176-
If ``True`` use constrained layout to adjust positioning of plot
2177-
elements. Like ``tight_layout``, but designed to be more
2178-
flexible. See
2179-
:doc:`/tutorials/intermediate/constrainedlayout_guide`
2180-
for examples. (Note: does not work with `add_subplot` or
2181-
`~.pyplot.subplot2grid`.)
2180+
This is equal to ``layout='constrained'``.
2181+
2182+
.. admonition:: Discouraged
2183+
2184+
The use of this parameter is discouraged. Please use
2185+
``layout='constrained'`` instead.
2186+
2187+
layout : {'constrained', 'tight'}, optional
2188+
The layout mechanism for positioning of plot elements.
2189+
Supported values:
2190+
2191+
- 'constrained': The constrained layout solver usually gives the
2192+
best layout results and is thus recommended. However, it is
2193+
computationally expensive and can be slow for complex figures
2194+
with many elements.
2195+
2196+
See :doc:`/tutorials/intermediate/constrainedlayout_guide`
2197+
for examples.
2198+
2199+
- 'tight': Use the tight layout mechanism. This is a relatively
2200+
simple algorithm, that adjusts the subplot parameters so that
2201+
decorations like tick labels, axis labels and titles have enough
2202+
space. See `.Figure.set_tight_layout` for further details.
2203+
2204+
If not given, fall back to using the parameters *tight_layout* and
2205+
*constrained_layout*, including their config defaults
2206+
:rc:`figure.autolayout` and :rc:`figure.constrained_layout.use`.
21822207
"""
21832208
super().__init__()
21842209

2210+
if layout is not None:
2211+
if tight_layout is not None:
2212+
_api.warn_external(
2213+
"The Figure parameters 'layout' and 'tight_layout' "
2214+
"cannot be used together. Please use 'layout' only.")
2215+
if constrained_layout is not None:
2216+
_api.warn_external(
2217+
"The Figure parameters 'layout' and 'constrained_layout' "
2218+
"cannot be used together. Please use 'layout' only.")
2219+
if layout == 'constrained':
2220+
tight_layout = False
2221+
constrained_layout = True
2222+
elif layout == 'tight':
2223+
tight_layout = True
2224+
constrained_layout = False
2225+
else:
2226+
raise ValueError(f"Invalid value for 'layout': {layout!r}")
2227+
21852228
self.callbacks = cbook.CallbackRegistry()
21862229
# Callbacks traditionally associated with the canvas (and exposed with
21872230
# a proxy property), but that actually need to be on the figure for
@@ -2344,7 +2387,7 @@ def set_tight_layout(self, tight):
23442387
----------
23452388
tight : bool or dict with keys "pad", "w_pad", "h_pad", "rect" or None
23462389
If a bool, sets whether to call `.tight_layout` upon drawing.
2347-
If ``None``, use the ``figure.autolayout`` rcparam instead.
2390+
If ``None``, use :rc:`figure.autolayout` instead.
23482391
If a dict, pass it as kwargs to `.tight_layout`, overriding the
23492392
default paddings.
23502393
"""

0 commit comments

Comments
 (0)