matplotlib.gridspec.GridSpec#

classmatplotlib.gridspec.GridSpec(nrows,ncols,figure=None,left=None,bottom=None,right=None,top=None,wspace=None,hspace=None,width_ratios=None,height_ratios=None)[source]#

Bases:GridSpecBase

A grid layout to place subplots within a figure.

The location of the grid cells is determined in a similar way toSubplotParams usingleft,right,top,bottom,wspaceandhspace.

Indexing a GridSpec instance returns aSubplotSpec.

Parameters:
nrows, ncolsint

The number of rows and columns of the grid.

figureFigure, optional

Only used for constrained layout to create a proper layoutgrid.

left, right, top, bottomfloat, optional

Extent of the subplots as a fraction of figure width or height.Left cannot be larger than right, and bottom cannot be larger thantop. If not given, the values will be inferred from a figure orrcParams at draw time. See alsoGridSpec.get_subplot_params.

wspacefloat, optional

The amount of width reserved for space between subplots,expressed as a fraction of the average axis width.If not given, the values will be inferred from a figure orrcParams when necessary. See alsoGridSpec.get_subplot_params.

hspacefloat, optional

The amount of height reserved for space between subplots,expressed as a fraction of the average axis height.If not given, the values will be inferred from a figure orrcParams when necessary. See alsoGridSpec.get_subplot_params.

width_ratiosarray-like of lengthncols, optional

Defines the relative widths of the columns. Each column gets arelative width ofwidth_ratios[i]/sum(width_ratios).If not given, all columns will have the same width.

height_ratiosarray-like of lengthnrows, optional

Defines the relative heights of the rows. Each row gets arelative height ofheight_ratios[i]/sum(height_ratios).If not given, all rows will have the same height.

get_subplot_params(figure=None)[source]#

Return theSubplotParams for the GridSpec.

In order of precedence the values are taken from

Note that thefigure attribute of the GridSpec is always ignored.

locally_modified_subplot_params()[source]#

Return a list of the names of the subplot parameters explicitly setin the GridSpec.

This is a subset of the attributes ofSubplotParams.

tight_layout(figure,renderer=None,pad=1.08,h_pad=None,w_pad=None,rect=None)[source]#

Adjust subplot parameters to give specified padding.

Parameters:
figureFigure

The figure.

rendererRendererBase subclass, optional

The renderer to be used.

padfloat

Padding between the figure edge and the edges of subplots, as afraction of the font-size.

h_pad, w_padfloat, optional

Padding (height/width) between edges of adjacent subplots.Defaults topad.

recttuple (left, bottom, right, top), default: None

(left, bottom, right, top) rectangle in normalized figurecoordinates that the whole subplots area (including labels) willfit into. Default (None) is the whole figure.

update(*,left=<UNSET>,bottom=<UNSET>,right=<UNSET>,top=<UNSET>,wspace=<UNSET>,hspace=<UNSET>)[source]#

Update the subplot parameters of the grid.

Parameters that are not explicitly given are not changed. Setting aparameter toNone resets it torcParams["figure.subplot.*"].

Parameters:
left, right, top, bottomfloat or None, optional

Extent of the subplots as a fraction of figure width or height.

wspace, hspacefloat or None, optional

Spacing between the subplots as a fraction of the average subplotwidth / height.

Examples usingmatplotlib.gridspec.GridSpec#

Custom spines with axisartist

Custom spines with axisartist

Streamplot

Streamplot

Resize Axes with constrained layout

Resize Axes with constrained layout

Resize Axes with tight layout

Resize Axes with tight layout

Combine two subplots using subplots and GridSpec

Combine two subplots using subplots and GridSpec

GridSpec with variable sizes and spacing

GridSpec with variable sizes and spacing

Gridspec for multi-column/row subplot layouts

Gridspec for multi-column/row subplot layouts

Nested Gridspecs

Nested Gridspecs

Figure subfigures

Figure subfigures

Create multiple subplots using plt.subplots

Create multiple subplots using plt.subplots

origin and extent in imshow

origin and extent in imshow

Arranging multiple Axes in a Figure

Arranging multiple Axes in a Figure

Constrained layout guide

Constrained layout guide

Tight layout guide

Tight layout guide