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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/AdvancedPlottingGroup(["`Advanced Plotting`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) 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`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/AdvancedPlottingGroup -.-> matplotlib/subplots("`Subplots`") matplotlib/PlotCustomizationGroup -.-> matplotlib/line_styles_colors("`Customizing Line Styles and Colors`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") 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-48890{{"`Creating Matplotlib Subplots in Python`"}} matplotlib/figures_axes -.-> lab-48890{{"`Creating Matplotlib Subplots in Python`"}} matplotlib/line_plots -.-> lab-48890{{"`Creating Matplotlib Subplots in Python`"}} matplotlib/subplots -.-> lab-48890{{"`Creating Matplotlib Subplots in Python`"}} matplotlib/line_styles_colors -.-> lab-48890{{"`Creating Matplotlib Subplots in Python`"}} python/tuples -.-> lab-48890{{"`Creating Matplotlib Subplots in Python`"}} python/function_definition -.-> lab-48890{{"`Creating Matplotlib Subplots in Python`"}} python/importing_modules -.-> lab-48890{{"`Creating Matplotlib Subplots in Python`"}} python/numerical_computing -.-> lab-48890{{"`Creating Matplotlib Subplots in Python`"}} python/data_visualization -.-> lab-48890{{"`Creating Matplotlib Subplots in Python`"}} end

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