Matplotlib Nested Gridspecs Visualization

PythonPythonBeginner
Practice Now

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

Introduction

Matplotlib is a data visualization library in Python. It provides a wide variety of charts and graphs that can be used to represent data in various forms. In this lab, we will go through the process of creating nested gridspecs using 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`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) 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`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48759{{"`Matplotlib Nested Gridspecs Visualization`"}} matplotlib/figures_axes -.-> lab-48759{{"`Matplotlib Nested Gridspecs Visualization`"}} python/booleans -.-> lab-48759{{"`Matplotlib Nested Gridspecs Visualization`"}} python/for_loops -.-> lab-48759{{"`Matplotlib Nested Gridspecs Visualization`"}} python/lists -.-> lab-48759{{"`Matplotlib Nested Gridspecs Visualization`"}} python/tuples -.-> lab-48759{{"`Matplotlib Nested Gridspecs Visualization`"}} python/function_definition -.-> lab-48759{{"`Matplotlib Nested Gridspecs Visualization`"}} python/importing_modules -.-> lab-48759{{"`Matplotlib Nested Gridspecs Visualization`"}} python/data_visualization -.-> lab-48759{{"`Matplotlib Nested Gridspecs Visualization`"}} python/build_in_functions -.-> lab-48759{{"`Matplotlib Nested Gridspecs Visualization`"}} end

Import Matplotlib Library

The first step is to import the Matplotlib library. We will also use the gridspec module from Matplotlib.

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

Create the Figure and Outer GridSpec

The next step is to create a figure and an outer gridspec. In this example, we will create a 1 by 2 gridspec.

fig = plt.figure()
gs0 = gridspec.GridSpec(1, 2, figure=fig)

Create the Inner GridSpec

Now, we will create the inner gridspec. We will use the GridSpecFromSubplotSpec method to create a 3 by 3 gridspec that will be a subplot of the outer gridspec.

gs00 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[0])

Add Subplots to the Inner GridSpec

We will now add subplots to the inner gridspec. We will create three subplots: ax1, ax2, and ax3.

ax1 = fig.add_subplot(gs00[:-1, :])
ax2 = fig.add_subplot(gs00[-1, :-1])
ax3 = fig.add_subplot(gs00[-1, -1])

Create Another Inner GridSpec

We will now create another inner gridspec. This time, we will use the subgridspec method to create a 3 by 3 gridspec that will be a subplot of the second column of the outer gridspec.

gs01 = gs0[1].subgridspec(3, 3)

Add Subplots to the Second Inner GridSpec

We will now add subplots to the second inner gridspec. We will create three subplots: ax4, ax5, and ax6.

ax4 = fig.add_subplot(gs01[:, :-1])
ax5 = fig.add_subplot(gs01[:-1, -1])
ax6 = fig.add_subplot(gs01[-1, -1])

Format the Axes

We will format the axes of all the subplots using the format_axes function. This function will add a text label to each subplot and remove the tick labels.

def format_axes(fig):
    for i, ax in enumerate(fig.axes):
        ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
        ax.tick_params(labelbottom=False, labelleft=False)

format_axes(fig)

Display the Figure

Finally, we will display the figure using the show method.

plt.show()

Summary

In this lab, we learned how to create nested gridspecs using Matplotlib. We created an outer gridspec and two inner gridspecs to create a complex layout of subplots. We also learned how to format the axes of the subplots using a custom function.

Other Python Tutorials you may like