Creating Matplotlib Subplots in Python

PythonPythonBeginner
Practice Now

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

Introduction

Matplotlib is a Python library used for creating visualizations such as line plots, scatter plots, bar plots, and subplots, among others. In this lab, you will learn how to create a figure with two subplots using .pyplot.subplot.

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 the necessary libraries

For this tutorial, we will be using the pyplot module from the Matplotlib library and the numpy library.

import matplotlib.pyplot as plt
import numpy as np

Define the data

We will define two sets of data that we will use to create our subplots.

def f(t):
    return np.exp(-t) * np.cos(2*np.pi*t)

t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)

Create the subplots

We will create a figure with two subplots using .pyplot.subplot.

plt.figure()

plt.subplot(211)
plt.plot(t1, f(t1), color='tab:blue', marker='o')
plt.plot(t2, f(t2), color='black')

plt.subplot(212)
plt.plot(t2, np.cos(2*np.pi*t2), color='tab:orange', linestyle='--')

plt.show()

The subplot() function takes three arguments: the number of rows, the number of columns, and the index of the current plot. The index starts from 1 in the upper left corner and increases row-wise. In this example, we create a figure with two subplots: one on top and one on the bottom.

In the first subplot, we plot t1 against f(t1) and t2 against f(t2). We set the color of the first plot to blue and add circular markers to each data point. We set the color of the second plot to black.

In the second subplot, we plot t2 against the cosine function of 2*np.pi*t2. We set the color of the plot to orange and the linestyle to dashed.

References

The use of the following functions, methods, classes, and modules is shown in this example:

  • matplotlib.pyplot.figure
  • matplotlib.pyplot.subplot

Summary

In this lab, we learned how to create a figure with two subplots using .pyplot.subplot. We defined two sets of data and plotted them in two subplots using different colors and line styles. We also provided references to the functions and modules used in this example.

Other Python Tutorials you may like