matplotlib.layout_engine — Matplotlib 3.10.1 documentation (original) (raw)
Classes to layout elements in a Figure.
Figures have a layout_engine
property that holds a subclass ofLayoutEngine defined here (or None for no layout). At draw timefigure.get_layout_engine().execute()
is called, the goal of which is usually to rearrange Axes on the figure to produce a pleasing layout. This is like a draw
callback but with two differences. First, when printing we disable the layout engine for the final draw. Second, it is useful to know the layout engine while the figure is being created. In particular, colorbars are made differently with different layout engines (for historical reasons).
Matplotlib supplies two layout engines, TightLayoutEngine andConstrainedLayoutEngine. Third parties can create their own layout engine by subclassing LayoutEngine.
class matplotlib.layout_engine.ConstrainedLayoutEngine(*, h_pad=None, w_pad=None, hspace=None, wspace=None, rect=(0, 0, 1, 1), compress=False, **kwargs)[source]#
Implements the constrained_layout
geometry management. SeeConstrained layout guide for details.
Initialize constrained_layout
settings.
Parameters:
h_pad, w_padfloat
Padding around the Axes elements in inches. Default to [rcParams["figure.constrained_layout.h_pad"]](../users/explain/customizing.html?highlight=figure.constrained%5Flayout.h%5Fpad#matplotlibrc-sample)
(default: 0.04167
) and[rcParams["figure.constrained_layout.w_pad"]](../users/explain/customizing.html?highlight=figure.constrained%5Flayout.w%5Fpad#matplotlibrc-sample)
(default: 0.04167
).
hspace, wspacefloat
Fraction of the figure to dedicate to space between the axes. These are evenly spread between the gaps between the Axes. A value of 0.2 for a three-column layout would have a space of 0.1 of the figure width between each column. If h/wspace < h/w_pad, then the pads are used instead. Default to [rcParams["figure.constrained_layout.hspace"]](../users/explain/customizing.html?highlight=figure.constrained%5Flayout.hspace#matplotlibrc-sample)
(default: 0.02
) and[rcParams["figure.constrained_layout.wspace"]](../users/explain/customizing.html?highlight=figure.constrained%5Flayout.wspace#matplotlibrc-sample)
(default: 0.02
).
recttuple of 4 floats
Rectangle in figure coordinates to perform constrained layout in (left, bottom, width, height), each from 0-1.
compressbool
Whether to shift Axes so that white space in between them is removed. This is useful for simple grids of fixed-aspect Axes (e.g. a grid of images). See Grids of fixed aspect-ratio Axes: "compressed" layout.
property adjust_compatible#
Return a boolean if the layout engine is compatible withsubplots_adjust.
property colorbar_gridspec#
Return a boolean if the layout engine creates colorbars using a gridspec.
Perform constrained_layout and move and resize Axes accordingly.
Parameters:
figFigure to perform layout on.
Return copy of the parameters for the layout engine.
set(*, h_pad=None, w_pad=None, hspace=None, wspace=None, rect=None)[source]#
Set the pads for constrained_layout.
Parameters:
h_pad, w_padfloat
Padding around the Axes elements in inches. Default to [rcParams["figure.constrained_layout.h_pad"]](../users/explain/customizing.html?highlight=figure.constrained%5Flayout.h%5Fpad#matplotlibrc-sample)
(default: 0.04167
) and[rcParams["figure.constrained_layout.w_pad"]](../users/explain/customizing.html?highlight=figure.constrained%5Flayout.w%5Fpad#matplotlibrc-sample)
(default: 0.04167
).
hspace, wspacefloat
Fraction of the figure to dedicate to space between the axes. These are evenly spread between the gaps between the Axes. A value of 0.2 for a three-column layout would have a space of 0.1 of the figure width between each column. If h/wspace < h/w_pad, then the pads are used instead. Default to [rcParams["figure.constrained_layout.hspace"]](../users/explain/customizing.html?highlight=figure.constrained%5Flayout.hspace#matplotlibrc-sample)
(default: 0.02
) and[rcParams["figure.constrained_layout.wspace"]](../users/explain/customizing.html?highlight=figure.constrained%5Flayout.wspace#matplotlibrc-sample)
(default: 0.02
).
recttuple of 4 floats
Rectangle in figure coordinates to perform constrained layout in (left, bottom, width, height), each from 0-1.
class matplotlib.layout_engine.LayoutEngine(**kwargs)[source]#
Base class for Matplotlib layout engines.
A layout engine can be passed to a figure at instantiation or at any time with set_layout_engine. Once attached to a figure, the layout engine execute
function is called at draw time bydraw, providing a special draw-time hook.
Note
However, note that layout engines affect the creation of colorbars, soset_layout_engine should be called before any colorbars are created.
Currently, there are two properties of LayoutEngine classes that are consulted while manipulating the figure:
engine.colorbar_gridspec
tells Figure.colorbar whether to make the
axes using the gridspec method (see colorbar.make_axes_gridspec) or not (see colorbar.make_axes);engine.adjust_compatible
stops Figure.subplots_adjust from being
run if it is not compatible with the layout engine.
To implement a custom LayoutEngine:
- override
_adjust_compatible
and_colorbar_gridspec
- override LayoutEngine.set to update self._params
- override LayoutEngine.execute with your implementation
property adjust_compatible#
Return a boolean if the layout engine is compatible withsubplots_adjust.
property colorbar_gridspec#
Return a boolean if the layout engine creates colorbars using a gridspec.
Execute the layout on the figure given by fig.
Return copy of the parameters for the layout engine.
Set the parameters for the layout engine.
class matplotlib.layout_engine.PlaceHolderLayoutEngine(adjust_compatible, colorbar_gridspec, **kwargs)[source]#
This layout engine does not adjust the figure layout at all.
The purpose of this LayoutEngine is to act as a placeholder when the user removes a layout engine to ensure an incompatible LayoutEngine cannot be set later.
Parameters:
adjust_compatible, colorbar_gridspecbool
Allow the PlaceHolderLayoutEngine to mirror the behavior of whatever layout engine it is replacing.
property adjust_compatible#
Return a boolean if the layout engine is compatible withsubplots_adjust.
property colorbar_gridspec#
Return a boolean if the layout engine creates colorbars using a gridspec.
Do nothing.
Return copy of the parameters for the layout engine.
Set the parameters for the layout engine.
class matplotlib.layout_engine.TightLayoutEngine(*, pad=1.08, h_pad=None, w_pad=None, rect=(0, 0, 1, 1), **kwargs)[source]#
Implements the tight_layout
geometry management. SeeTight layout guide for details.
Initialize tight_layout engine.
Parameters:
padfloat, default: 1.08
Padding between the figure edge and the edges of subplots, as a fraction of the font size.
h_pad, w_padfloat
Padding (height/width) between edges of adjacent subplots. Defaults to pad.
recttuple (left, bottom, right, top), default: (0, 0, 1, 1).
rectangle in normalized figure coordinates that the subplots (including labels) will fit into.
property adjust_compatible#
Return a boolean if the layout engine is compatible withsubplots_adjust.
property colorbar_gridspec#
Return a boolean if the layout engine creates colorbars using a gridspec.
Execute tight_layout.
This decides the subplot parameters given the padding that will allow the Axes labels to not be covered by other labels and Axes.
Parameters:
figFigure to perform layout on.
Return copy of the parameters for the layout engine.
set(*, pad=None, w_pad=None, h_pad=None, rect=None)[source]#
Set the pads for tight_layout.
Parameters:
padfloat
Padding between the figure edge and the edges of subplots, as a fraction of the font size.
w_pad, h_padfloat
Padding (width/height) between edges of adjacent subplots. Defaults to pad.
recttuple (left, bottom, right, top)
rectangle in normalized figure coordinates that the subplots (including labels) will fit into.