If you are reading this book from the beginning, you are probably familiar with the subplot
class, a descendant of axes
that lives on the regular grid of subplot
instances. We are going to explain and demonstrate how to use subplots in advanced ways.
In this recipe, you will be learning how to create custom subplot configurations on our plots.
The base class for subplots is matplotlib.axes.SubplotBase
. These subplots are matplotlib.axes.Axes
instances, but provide helper methods for generating and manipulating a set of Axes
within a figure.
There is a class matplotlib.figure.SubplotParams
, which holds all the parameters for subplot
. The dimensions are normalized to the width or height of the figure. As we already know, if we don't specify any custom values, they will be read from the rc
parameters.
The scripting layer (matplotlib.pyplot
) holds a few helper methods to manipulate subplots.
matplotlib.pyplot.subplots
is used for the easy creation of common layouts of subplots. We can specify the size of the grid—the number of rows and columns of the subplot grid.
We can create subplots that share the X or y axes. This is achieved using sharex
or the sharey
keyword argument. The sharex
argument can have the True
value, in which case the X axis is shared among all the subplots. The tick labels will be invisible on all but the last row of plots. They can also be defined as String, with enumerated values of row
, col
, all
, or none
. The all
value is the same as True
, and the value none
is the same as False
. If the value row
is specified, each subplot row shares the X axis. If the value col
is specified, each subplot column shares the X axis. This helper returns tuple fig, ax
, where ax
is either an axis instance or, if more than one subplot is created, an array of axis instances.
matplotlib.pyplot.subplots_adjust
is used to tune the subplot layout. The keyword arguments specify the coordinates of the subplots inside the figure (left
, right
, bottom
, and top
) normalized to figure size. White space can be specified to be left between the subplots using the wspace
and hspace
arguments for width and height amounts, respectively.
subplot2grid
. We define the grid's geometry and the subplot location. Note that this location is 0-based not 1-based as we are used to in plot.subplot()
. We can also use colspan
and rowspan
to allow the subplot to span multiple columns and rows in a given grid. For example, we will create a figure, add various subplot layouts using subplot2grid
, and reconfigure the tick label size.import matplotlib.pyplot as plt plt.figure(0) axes1 = plt.subplot2grid((3, 3), (0, 0), colspan=3) axes2 = plt.subplot2grid((3, 3), (1, 0), colspan=2) axes3 = plt.subplot2grid((3, 3), (1, 2)) axes4 = plt.subplot2grid((3, 3), (2, 0)) axes5 = plt.subplot2grid((3, 3), (2, 1), colspan=2) # tidy up tick labels size all_axes = plt.gcf().axes for ax in all_axes: forticklabel in ax.get_xticklabels() + ax.get_yticklabels(): ticklabel.set_fontsize(10) plt.suptitle("Demo of subplot2grid") plt.show()
When we execute the previous code, the following plot is created:
We provide subplot2grid
with a shape, location (loc
), and optionally, rowspan
and colspan
. The important difference here is that the location is indexed from 0, and not from 1, as in figure.add_subplot
.
To give an example of another way, you can customize the current axes
or subplot
:
axes = fig.add_subplot(111) rectangle = axes.patch rectangle.set_facecolor('blue')
Here we see that every axes
instance contains a field patch referencing the rectangle
instance, thus representing the background of the current axes
instance. This instance has properties that we can update, hence updating the current axes
background. We can change its color, but we can also load an image to add a watermark protection, for example.
It is also possible to create a patch first and then just add it to the axes
background:
fig = plt.figure() axes = fig.add_subplot(111) rect = matplotlib.patches.Rectangle((1,1), width=6, height=12) axes.add_patch(rect) # we have to manually force a figure draw axes.figure.canvas.draw()