Skip to content

Commit b92db60

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

File tree

2 files changed

+72
-12
lines changed

2 files changed

+72
-12
lines changed
Lines changed: 13 additions & 0 deletions
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

Lines changed: 59 additions & 12 deletions
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,67 @@ 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'} or dict, 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' or dict: Use the tight layout mechanism. This is a
2185+
relatively simple algorithm, that adjusts the subplot parameters
2186+
so that decorations like tick labels, axis labels and titles
2187+
have enough space. See `.Figure.set_tight_layout` for further
2188+
details.
2189+
2190+
If not given, fall back to using the parameters *tight_layout* and
2191+
*constrained_layout*, including their config defaults
2192+
:rc:`figure.autolayout` and :rc:`figure.constrained_layout.use`.
21672193
"""
21682194
super().__init__()
21692195

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

0 commit comments

Comments
 (0)