You are reading an old version of the documentation (v3.1.1). For the latest version see
Version 3.1.2
Fork me on GitHub

Table of Contents


class matplotlib.gridspec.SubplotSpec(gridspec, num1, num2=None)[source]

Bases: object

Specifies the location of a subplot in a GridSpec.


Likely, you'll never instantiate a SubplotSpec yourself. Instead you will typically obtain one from a GridSpec using item-access.

gridspec : GridSpec

The GridSpec, which the subplot is referencing.

num1, num2 : int

The subplot will occupy the num1-th cell of the given gridspec. If num2 is provided, the subplot will span between num1-th cell and num2-th cell.

The index starts from 0.


Return the subplot geometry as tuple (n_rows, n_cols, start, stop).

The indices start and stop define the range of the subplot within the GridSpec. stop is inclusive (i.e. for a single cell start == stop).

get_position(self, figure, return_all=False)[source]

Update the subplot position from figure.subplotpars.


Return the subplot row and column numbers as a tuple (n_rows, n_cols, row_start, row_stop, col_start, col_stop).


Return the topmost SubplotSpec instance associated with the subplot.

subgridspec(self, nrows, ncols, **kwargs)[source]

Create a GridSpec within this subplot.

The created GridSpecFromSubplotSpec will have this SubplotSpec as a parent.

nrows : int

Number of rows in grid.

ncols : int

Number or columns in grid.

gridspec : GridSpecFromSubplotSpec
Other Parameters:

All other parameters are passed to GridSpecFromSubplotSpec.


Adding three subplots in the space occupied by a single subplot:

fig = plt.figure()
gs0 = fig.add_gridspec(3, 1)
ax1 = fig.add_subplot(gs0[0])
ax2 = fig.add_subplot(gs0[1])
gssub = gs0[2].subgridspec(1, 3)
for i in range(3):
    fig.add_subplot(gssub[0, i])

Examples using matplotlib.gridspec.SubplotSpec