Often more than one Axes is wanted on a figure at a time, usually
organized into a regular grid. Matplotlib has a variety of tools for
working with grids of Axes that have evolved over the history of the library.
Here we will discuss the tools we think users should use most often, the tools
that underpin how Axes are organized, and mention some of the older tools.
Overview
Create grid-shaped combinations of Axes
subplots
The primary function used to create figures and a grid of Axes. It
creates and places all Axes on the figure at once, and returns an
object array with handles for the Axes in the grid. See
Figure.subplots.
Specifies the geometry of the grid that a subplot will be
placed. The number of rows and number of columns of the grid
need to be set. Optionally, the subplot layout parameters
(e.g., left, right, etc.) can be tuned.
Specifies the location of the subplot in the given GridSpec.
Adding single Axes at a time
The above functions create all Axes in a single function call. It is also
possible to add Axes one at a time, and this was originally how Matplotlib
used to work. Doing so is generally less elegant and flexible, though
sometimes useful for interactive work or to place an Axes in a custom
location:
Similar to pyplot.subplot, but uses 0-based indexing and two-d python
slicing to choose cells.
As a simple example of manually adding an Axes ax, lets add a 3 inch x 2 inch
Axes to a 4 inch x 3 inch figure. Note that the location of the subplot is
defined as [left, bottom, width, height] in figure-normalized units:
We can create a basic 2-by-2 grid of Axes using
subplots. It returns a Figure
instance and an array of Axes objects. The Axes
objects can be used to access methods to place artists on the Axes; here
we use annotate, but other examples could be plot,
pcolormesh, etc.
The same effect can be achieved with subplot_mosaic,
but the return type is a dictionary instead of an array, where the user
can give the keys useful meanings. Here we provide two lists, each list
representing a row, and each element in the list a key representing the
column.
Fixed-aspect ratio Axes are common for images or maps. However, they
present a challenge to layout because two sets of constraints are being
imposed on the size of the Axes - that they fit in the figure and that they
have a set aspect ratio. This leads to large gaps between Axes by default:
Sometimes we want Axes to span rows or columns of the grid.
There are actually multiple ways to accomplish this, but the most
convenient is probably to use subplot_mosaic by repeating one
of the keys:
Both subplots and subplot_mosaic allow the rows
in the grid to be different heights, and the columns to be different
widths using the gridspec_kw keyword argument.
Spacing parameters accepted by GridSpec
can be passed to subplots and
subplot_mosaic:
Nested Axes layouts
Sometimes it is helpful to have two or more grids of Axes that
may not need to be related to one another. The most simple way to
accomplish this is to use Figure.subfigures. Note that the subfigure
layouts are independent, so the Axes spines in each subfigure are not
necessarily aligned. See below for a more verbose way to achieve the same
effect with GridSpecFromSubplotSpec.
Internally, the arrangement of a grid of Axes is controlled by creating
instances of GridSpec and SubplotSpec. GridSpec defines a
(possibly non-uniform) grid of cells. Indexing into the GridSpec returns
a SubplotSpec that covers one or more grid cells, and can be used to
specify the location of an Axes.
The following examples show how to use low-level methods to arrange Axes
using GridSpec objects.
Basic 2x2 grid
We can accomplish a 2x2 grid in the same manner as
plt.subplots(2,2):
We can index the spec array using NumPy slice syntax
and the new Axes will span the slice. This would be the same
as fig,axd=plt.subplot_mosaic([['ax0','ax0'],['ax1','ax2']],...):
When a GridSpec is explicitly used, you can adjust the layout
parameters of subplots that are created from the GridSpec. Note this
option is not compatible with constrained layout or
Figure.tight_layout which both ignore left and right and adjust
subplot sizes to fill the figure. Usually such manual placement
requires iterations to make the Axes tick labels not overlap the Axes.
These spacing parameters can also be passed to subplots and
subplot_mosaic as the gridspec_kw argument.
Nested layouts with SubplotSpec
You can create nested layout similar to subfigures using
subgridspec. Here the Axes spines are
aligned.
One way to address this is to change the aspect of the figure to be close
to the aspect ratio of the Axes, however that requires trial and error.
Matplotlib also supplies layout="compressed", which will work with
simple grids to reduce the gaps between Axes. (The mpl_toolkits also
provides ImageGrid to accomplish
a similar effect, but with a non-standard Axes class).
It is also possible to nest Axes using subplot_mosaic using
nested lists. This method does not use subfigures, like above, so lacks
the ability to add per-subfigure suptitle and supxlabel, etc.
Rather it is a convenience wrapper around the subgridspec
method described below.
Here's a more sophisticated example of nested GridSpec: We create an outer
4x4 grid with each cell containing an inner 3x3 grid of Axes. We outline
the outer 4x4 grid by hiding appropriate spines in each of the inner 3x3
grids.