Create Matplotlib Plot Legends

PythonPythonBeginner
Practice Now

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

Introduction

Matplotlib is a Python library that enables users to create various types of graphs and plots. This tutorial will guide you through the process of creating a legend in a Matplotlib plot.

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`"]) 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/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`") matplotlib/PlotCustomizationGroup -.-> matplotlib/legend_config("`Legend Configuration`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-48941{{"`Create Matplotlib Plot Legends`"}} matplotlib/importing_matplotlib -.-> lab-48941{{"`Create Matplotlib Plot Legends`"}} matplotlib/figures_axes -.-> lab-48941{{"`Create Matplotlib Plot Legends`"}} matplotlib/line_plots -.-> lab-48941{{"`Create Matplotlib Plot Legends`"}} matplotlib/legend_config -.-> lab-48941{{"`Create Matplotlib Plot Legends`"}} python/lists -.-> lab-48941{{"`Create Matplotlib Plot Legends`"}} python/tuples -.-> lab-48941{{"`Create Matplotlib Plot Legends`"}} python/importing_modules -.-> lab-48941{{"`Create Matplotlib Plot Legends`"}} python/data_visualization -.-> lab-48941{{"`Create Matplotlib Plot Legends`"}} end

Import necessary libraries

Before we start, we need to import the necessary libraries. In this case, we will be using the Matplotlib library.

import matplotlib.pyplot as plt

Create a figure and subplot

We need to create a figure and subplot to plot our data. We will be creating a plot with two subplots.

fig = plt.figure()

ax = fig.add_subplot(211)
ax.plot([1, 2, 3], label="test1")
ax.plot([3, 2, 1], label="test2")

ax = fig.add_subplot(223)
ax.plot([1, 2, 3], label="test1")
ax.plot([3, 2, 1], label="test2")

Add a legend to the plot

We will now add a legend to the plot. There are two ways to add a legend in Matplotlib. We will use both methods in this example.

## Method 1: Place a legend above the subplot
ax.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc='lower left',
           ncols=2, mode="expand", borderaxespad=0.)

## Method 2: Place a legend to the right of the subplot
ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.)

Display the plot

Finally, we will display the plot.

plt.show()

Summary

In this tutorial, we learned how to add a legend to a Matplotlib plot. We used two different methods to add a legend. The first method placed the legend above the subplot, while the second method placed the legend to the right of the subplot.

Other Python Tutorials you may like