Matplotlib Nested GridSpecs

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, you will learn how to use nested .GridSpecs in Matplotlib to create a grid of subplots with varying sizes. This is useful when you want to create a complex layout of plots and have control over the size and spacing of each plot.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Import Libraries

First, we need to import the necessary libraries. We will be using matplotlib.pyplot for creating the plots and numpy for generating some data to plot.

import matplotlib.pyplot as plt
import numpy as np

Create Data

In this step, we will create some data to plot. We will use the squiggle_xy function to generate some sine and cosine waves with different frequencies.

def squiggle_xy(a, b, c, d):
    i = np.arange(0.0, 2*np.pi, 0.05)
    return np.sin(i*a)*np.cos(i*b), np.sin(i*c)*np.cos(i*d)

Create the Figure and Outer Grid

Next, we will create the figure and the outer grid using the add_gridspec function. We will create a 4x4 grid with no spacing between the subplots.

fig = plt.figure(figsize=(8, 8))
outer_grid = fig.add_gridspec(4, 4, wspace=0, hspace=0)

Create the Inner Grids and Subplots

In this step, we will create the inner grids and subplots using nested .GridSpecs. We will loop through each cell in the outer grid and create a 3x3 grid for each cell.

for a in range(4):
    for b in range(4):
        ## gridspec inside gridspec
        inner_grid = outer_grid[a, b].subgridspec(3, 3, wspace=0, hspace=0)
        axs = inner_grid.subplots()  ## Create all subplots for the inner grid.
        for (c, d), ax in np.ndenumerate(axs):
            ax.plot(*squiggle_xy(a + 1, b + 1, c + 1, d + 1))
            ax.set(xticks=[], yticks=[])

Show Only the Outer Spines

In this step, we will remove the spines for the inner subplots and only show the outer spines. This will make the plot look cleaner.

for ax in fig.get_axes():
    ss = ax.get_subplotspec()
    ax.spines.top.set_visible(ss.is_first_row())
    ax.spines.bottom.set_visible(ss.is_last_row())
    ax.spines.left.set_visible(ss.is_first_col())
    ax.spines.right.set_visible(ss.is_last_col())

Display the Plot

Finally, we will display the plot using the show() function.

plt.show()

Summary

In this lab, you learned how to use nested .GridSpecs in Matplotlib to create a grid of subplots with varying sizes. We also learned how to generate data using numpy and how to customize the spines of the subplots. With this knowledge, you can create complex layouts of plots with precise control over their size and spacing.

Other Python Tutorials you may like