matplotlib.layout_engine#
Classes to layout elements in a Figure.
Figures have a layout_engine property that holds a subclass of
LayoutEngine defined here (or None for no layout). At draw time
figure.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 has two built-in layout engines:
TightLayoutEnginewas the first layout engine added to Matplotlib. See also Tight layout guide.ConstrainedLayoutEngineis more modern and generally gives better results. See also Constrained layout guide.
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_layoutgeometry management. See Constrained layout guide for details.Initialize
constrained_layoutsettings.- Parameters:
- h_pad, w_padfloat
Padding around the Axes elements in inches. Default to
rcParams["figure.constrained_layout.h_pad"](default:0.04167) andrcParams["figure.constrained_layout.w_pad"](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"](default:0.02) andrcParams["figure.constrained_layout.wspace"](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 with
subplots_adjust.
- property colorbar_gridspec#
Return a boolean if the layout engine creates colorbars using a gridspec.
- execute(fig)[source]#
Perform constrained_layout and move and resize Axes accordingly.
- Parameters:
- fig
Figureto perform layout on.
- fig
- set(*, h_pad=None, w_pad=None, hspace=None, wspace=None,