gridspec
Customizing Figure Layouts Using GridSpec and Other Functions
How to create grid-shaped combinations of axes.
1 | import matplotlib |
Basic Quickstart Guide
These first two examples show how to create a basic 2-by-2 grid using
both subplots() and gridspec.
Using subplots() is quite simple.
It returns a Figure instance and an array ofAxes objects.
1 | fig1, f1_axes = plt.subplots(ncols=2, nrows=2, constrained_layout=True) |

For a simple use case such as this, gridspec is
perhaps overly verbose.
You have to create the figure and GridSpec
instance separately, then pass elements of gridspec instance to theadd_subplot() method to create the axes
objects.
The elements of the gridspec are accessed in generally the same manner as
numpy arrays.
1 | fig2 = plt.figure(constrained_layout=True) |

The power of gridspec comes in being able to create subplots that span
rows and columns. Note the
Numpy slice
syntax for selecting the part of the gridspec each subplot will occupy.
Note that we have also used the convenience method Figure.add_gridspec
instead of gridspec.GridSpec, potentially saving the user an import,
and keeping the namespace cleaner.
1 | fig3 = plt.figure(constrained_layout=True) |

gridspec is also indispensable for creating subplots
of different widths via a couple of methods.
The method shown here is similar to the one above and initializes a
uniform grid specification,
and then uses numpy indexing and slices to allocate multiple
“cells” for a given subplot.
1 | fig4 = plt.figure(constrained_layout=True) |

Another option is to use the width_ratios and height_ratios
parameters. These keyword arguments are lists of numbers.
Note that absolute values are meaningless, only their relative ratios
matter. That means that width_ratios=[2, 4, 8] is equivalent towidth_ratios=[1, 2, 4] within equally wide figures.
For the sake of demonstration, we’ll blindly create the axes withinfor loops since we won’t need them later.
1 | fig5 = plt.figure(constrained_layout=True) |

Learning to use width_ratios and height_ratios is particularly
useful since the top-level function subplots()
accepts them within the gridspec_kw parameter.
For that matter, any parameter accepted byGridSpec can be passed tosubplots() via the gridspec_kw parameter.
This example recreates the previous figure without directly using a
gridspec instance.
1 | gs_kw = dict(width_ratios=widths, height_ratios=heights) |

The subplots and gridspec methods can be combined since it is
sometimes more convenient to make most of the subplots using subplots
and then remove some and combine them. Here we create a layout with
the bottom two axes in the last column combined.
1 | fig7, f7_axs = plt.subplots(ncols=3, nrows=3) |

Fine Adjustments to a Gridspec Layout
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 orFigure.tight_layout which both adjust subplot sizes to fill the
figure.
1 | fig8 = plt.figure(constrained_layout=False) |

This is similar to subplots_adjust(), but it only
affects the subplots that are created from the given GridSpec.
For example, compare the left and right sides of this figure:
1 | fig9 = plt.figure(constrained_layout=False) |

GridSpec using SubplotSpec
You can create GridSpec from the SubplotSpec,
in which case its layout parameters are set to that of the location of
the given SubplotSpec.
Note this is also available from the more verbosegridspec.GridSpecFromSubplotSpec.
1 | fig10 = plt.figure(constrained_layout=True) |

A Complex Nested GridSpec using SubplotSpec
Here’s a more sophisticated example of nested GridSpec where we put
a box around each cell of the outer 4x4 grid, by hiding appropriate
spines in each of the inner 3x3 grids.
1 | import numpy as np |

References
The usage of the following functions and methods is shown in this example:
1 | matplotlib.pyplot.subplots |
Total running time of the script: ( 0 minutes 8.732 seconds)





