Introduction
Matplotlib is a data visualization library for the Python programming language. It provides a wide range of tools for creating different types of plots and charts. The GridSpec module in Matplotlib allows us to create flexible and complex layouts of subplots. In this tutorial, we will learn how to use GridSpec to create multi-column/row subplot layouts.
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 necessary libraries
First, we need to import the required libraries. We will be using Matplotlib and GridSpec.
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
Create a figure
Next, we need to create a figure using the plt.figure() function. We can set the layout parameter to "constrained" to ensure that the subplots fit within the figure.
fig = plt.figure(layout="constrained")
Create a GridSpec
We can create a GridSpec object using the GridSpec() function. We need to specify the number of rows and columns we want in our grid. In this example, we will create a 3x3 grid.
gs = GridSpec(3, 3, figure=fig)
Add subplots to the GridSpec
We can add subplots to the GridSpec using the fig.add_subplot() function. We can specify the location of the subplot in the grid using the indexing notation of the GridSpec object. For example, gs[0, :] specifies the first row and all columns.
ax1 = fig.add_subplot(gs[0, :])
ax2 = fig.add_subplot(gs[1, :-1])
ax3 = fig.add_subplot(gs[1:, -1])
ax4 = fig.add_subplot(gs[-1, 0])
ax5 = fig.add_subplot(gs[-1, -2])
Customize subplots
We can customize the subplots as needed. For example, we can set the title of the figure using the fig.suptitle() function, and we can format the axes using the format_axes() function.
fig.suptitle("GridSpec")
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 plot
Finally, we can display the plot using the plt.show() function.
plt.show()
Summary
In this tutorial, we learned how to use GridSpec to create multi-column/row subplot layouts in Matplotlib. We created a 3x3 grid and added subplots to it. We customized the subplots and displayed the plot. GridSpec is a powerful tool for creating complex subplot layouts in Matplotlib.