Combining Subplots with GridSpec

PythonPythonBeginner
Practice Now

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

Introduction

Matplotlib is a popular Python library used for data visualization. In this lab, we will learn how to combine two subplots using subplots and GridSpec in Matplotlib.

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlotCustomizationGroup -.-> matplotlib/text_annotations("`Text Annotations`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48757{{"`Combining Subplots with GridSpec`"}} matplotlib/figures_axes -.-> lab-48757{{"`Combining Subplots with GridSpec`"}} matplotlib/text_annotations -.-> lab-48757{{"`Combining Subplots with GridSpec`"}} python/for_loops -.-> lab-48757{{"`Combining Subplots with GridSpec`"}} python/lists -.-> lab-48757{{"`Combining Subplots with GridSpec`"}} python/tuples -.-> lab-48757{{"`Combining Subplots with GridSpec`"}} python/importing_modules -.-> lab-48757{{"`Combining Subplots with GridSpec`"}} python/data_visualization -.-> lab-48757{{"`Combining Subplots with GridSpec`"}} end

Import Required Libraries

We start by importing the necessary libraries for this lab. We will be using Matplotlib for data visualization.

import matplotlib.pyplot as plt

Create a Figure with Subplots

We create a figure with three columns and three rows of subplots.

fig, axs = plt.subplots(ncols=3, nrows=3)

Get the GridSpec from the Axes

We get the GridSpec from the second row and third column of the subplots.

gs = axs[1, 2].get_gridspec()

Remove the Underlying Axes

We remove the underlying axes that are covered by the bigger axes that we will create in the next step.

for ax in axs[1:, -1]:
    ax.remove()

Add a Bigger Axes

We add a bigger axes that covers the second and third rows of the last column.

axbig = fig.add_subplot(gs[1:, -1])

Annotate the Bigger Axes

We annotate the bigger axes with some text.

axbig.annotate('Big Axes \nGridSpec[1:, -1]', (0.1, 0.5),
               xycoords='axes fraction', va='center')

Adjust the Layout

We adjust the layout of the subplots to ensure they fit in the figure.

fig.tight_layout()

Display the Plot

We display the plot using Matplotlib.

plt.show()

Summary

In this lab, we learned how to combine two subplots using subplots and GridSpec in Matplotlib. We created a figure with subplots, got the GridSpec from the axes, removed the underlying axes, added a bigger axes, annotated the bigger axes, and adjusted the layout of the subplots. Finally, we displayed the plot using Matplotlib.

Other Python Tutorials you may like