Matplotlib Subplot Creation Tutorial

PythonPythonBeginner
Practice Now

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

Introduction

In data visualization, it is often necessary to plot multiple graphs in a single figure. Matplotlib allows us to achieve this using subplots. In this lab, we will learn how to create subplots 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 python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-48966{{"`Matplotlib Subplot Creation Tutorial`"}} matplotlib/importing_matplotlib -.-> lab-48966{{"`Matplotlib Subplot Creation Tutorial`"}} matplotlib/figures_axes -.-> lab-48966{{"`Matplotlib Subplot Creation Tutorial`"}} matplotlib/line_plots -.-> lab-48966{{"`Matplotlib Subplot Creation Tutorial`"}} python/tuples -.-> lab-48966{{"`Matplotlib Subplot Creation Tutorial`"}} python/importing_modules -.-> lab-48966{{"`Matplotlib Subplot Creation Tutorial`"}} python/standard_libraries -.-> lab-48966{{"`Matplotlib Subplot Creation Tutorial`"}} python/numerical_computing -.-> lab-48966{{"`Matplotlib Subplot Creation Tutorial`"}} python/data_visualization -.-> lab-48966{{"`Matplotlib Subplot Creation Tutorial`"}} end

Import Libraries

First, we need to import the required libraries. We will be using Matplotlib and NumPy. NumPy is used to generate some sample data.

import matplotlib.pyplot as plt
import numpy as np

Generate Some Sample Data

We will generate some sample data that we will use to plot our graphs.

## Create some fake data.
x1 = np.linspace(0.0, 5.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
x2 = np.linspace(0.0, 2.0)
y2 = np.cos(2 * np.pi * x2)

Create Subplots Using subplots()

We will create subplots using subplots() function. We will create two subplots, one above the other.

## Create subplots
fig, (ax1, ax2) = plt.subplots(2, 1)

Set Title and Axes Labels

We will set the title and axis labels for our subplots.

## Set title and axis labels
fig.suptitle('A tale of 2 subplots')

ax1.set_ylabel('Damped oscillation')
ax2.set_xlabel('time (s)')
ax2.set_ylabel('Undamped')

Plot Data on Subplots

We will now plot our data on the subplots.

## Plot data on subplots
ax1.plot(x1, y1, 'o-')
ax2.plot(x2, y2, '.-')

Display the Plots

Finally, we will display the plots using plt.show().

## Display the plots
plt.show()

Summary

In this lab, we learned how to create subplots in Matplotlib. We used the subplots() function to create subplots and set the title, axis labels, and plot data on the subplots. By using subplots, we can display multiple graphs in a single figure, which is useful for data visualization.

Other Python Tutorials you may like