Creating Adjacent Subplots

PythonPythonBeginner
Practice Now

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

Introduction

In data visualization, it is often useful to create multiple plots that share a common axis. This can be achieved using the subplots function in Matplotlib. In this tutorial, we will learn how to create adjacent subplots that share a common x-axis.

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/PlottingDataGroup(["`Plotting Data`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) 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/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48751{{"`Creating Adjacent Subplots`"}} matplotlib/figures_axes -.-> lab-48751{{"`Creating Adjacent Subplots`"}} matplotlib/line_plots -.-> lab-48751{{"`Creating Adjacent Subplots`"}} python/booleans -.-> lab-48751{{"`Creating Adjacent Subplots`"}} python/lists -.-> lab-48751{{"`Creating Adjacent Subplots`"}} python/tuples -.-> lab-48751{{"`Creating Adjacent Subplots`"}} python/importing_modules -.-> lab-48751{{"`Creating Adjacent Subplots`"}} python/numerical_computing -.-> lab-48751{{"`Creating Adjacent Subplots`"}} python/data_visualization -.-> lab-48751{{"`Creating Adjacent Subplots`"}} end

Import Libraries

We begin by importing the necessary libraries - numpy and matplotlib.pyplot.

import numpy as np
import matplotlib.pyplot as plt

Generate Data

We generate some sample data to be plotted. Here, we use the numpy library to generate three arrays of data.

t = np.arange(0.0, 2.0, 0.01)

s1 = np.sin(2 * np.pi * t)
s2 = np.exp(-t)
s3 = s1 * s2

Create Subplots

We create three subplots using the subplots function in Matplotlib. We set the sharex parameter to True to ensure that the subplots share a common x-axis. We also remove the vertical space between the subplots using the subplots_adjust function.

fig, axs = plt.subplots(3, 1, sharex=True)
fig.subplots_adjust(hspace=0)

Plot Data

We plot the data on each subplot and set the y-tick values and limits for each plot.

axs[0].plot(t, s1)
axs[0].set_yticks(np.arange(-0.9, 1.0, 0.4))
axs[0].set_ylim(-1, 1)

axs[1].plot(t, s2)
axs[1].set_yticks(np.arange(0.1, 1.0, 0.2))
axs[1].set_ylim(0, 1)

axs[2].plot(t, s3)
axs[2].set_yticks(np.arange(-0.9, 1.0, 0.4))
axs[2].set_ylim(-1, 1)

Display Plot

We display the plot using the show function in Matplotlib.

plt.show()

Summary

In this tutorial, we learned how to create adjacent subplots that share a common x-axis using the subplots function in Matplotlib. We also learned how to set the y-tick values and limits for each plot. This technique is useful in data visualization to compare multiple datasets that share a common axis.

Other Python Tutorials you may like