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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/PlotCustomizationGroup -.-> matplotlib/titles_labels("`Adding Titles and Labels`") matplotlib/PlotCustomizationGroup -.-> matplotlib/legend_config("`Legend Configuration`") 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`") python/BasicConceptsGroup -.-> python/python_shell("`Python Shell`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48889{{"`Plotting Multiple Datasets`"}} matplotlib/figures_axes -.-> lab-48889{{"`Plotting Multiple Datasets`"}} matplotlib/line_plots -.-> lab-48889{{"`Plotting Multiple Datasets`"}} matplotlib/titles_labels -.-> lab-48889{{"`Plotting Multiple Datasets`"}} matplotlib/legend_config -.-> lab-48889{{"`Plotting Multiple Datasets`"}} python/tuples -.-> lab-48889{{"`Plotting Multiple Datasets`"}} python/importing_modules -.-> lab-48889{{"`Plotting Multiple Datasets`"}} python/numerical_computing -.-> lab-48889{{"`Plotting Multiple Datasets`"}} python/data_visualization -.-> lab-48889{{"`Plotting Multiple Datasets`"}} python/python_shell -.-> lab-48889{{"`Plotting Multiple Datasets`"}} end

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