Plotting Multiple Datasets

PythonPythonBeginner
Practice Now

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

Introduction

Matplotlib is a popular data visualization library in Python. In this lab, you will learn how to plot multiple datasets using a single call to the plot function 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.

Import the Required Libraries

In this step, we will import the necessary libraries, including numpy and matplotlib. We will also set up Matplotlib to display plots inline within the Jupyter Notebook.

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

Create the Data

In this step, we will create three different datasets using the arange function from NumPy. We will create a time series with 200ms intervals, ranging from 0 to 5 seconds.

t = np.arange(0., 5., 0.2)

Plot the Data

In this step, we will use the plot function in Matplotlib to plot all three datasets in a single call. We will use red dashes for the first dataset, blue squares for the second dataset, and green triangles for the third dataset.

plt.plot(t, t, 'r--', label='linear')
plt.plot(t, t**2, 'bs', label='quadratic')
plt.plot(t, t**3, 'g^', label='cubic')
plt.legend()
plt.show()

Add Labels and Titles

In this step, we will add a title to the plot and label the x and y axes.

plt.plot(t, t, 'r--', label='linear')
plt.plot(t, t**2, 'bs', label='quadratic')
plt.plot(t, t**3, 'g^', label='cubic')
plt.legend()
plt.title("Multiple Datasets")
plt.xlabel("Time (s)")
plt.ylabel("Value")
plt.show()

Summary

In this lab, you learned how to plot multiple datasets using a single call to the plot function in Matplotlib. You also learned how to add labels and titles to the plot to make it more informative. Matplotlib is a powerful library that offers many options for customizing plots and visualizations.

Other Python Tutorials you may like