Skip to main content
BlogWeb

Back to all posts

How to Display A Legend With Matplotlib?

Published on
3 min read
How to Display A Legend With Matplotlib? image

Best Matplotlib Tools to Buy in July 2026

1 Python Data Science Handbook: Essential Tools for Working with Data

Python Data Science Handbook: Essential Tools for Working with Data

BUY & SAVE
$52.15 $69.99
Save 25%
Python Data Science Handbook: Essential Tools for Working with Data
2 Python Data Cleaning Cookbook: Prepare your data for analysis with pandas, NumPy, Matplotlib, scikit-learn, and OpenAI

Python Data Cleaning Cookbook: Prepare your data for analysis with pandas, NumPy, Matplotlib, scikit-learn, and OpenAI

BUY & SAVE
$49.99
Python Data Cleaning Cookbook: Prepare your data for analysis with pandas, NumPy, Matplotlib, scikit-learn, and OpenAI
3 Hands-On Machine Learning with Scikit-Learn and PyTorch: Concepts, Tools, and Techniques to Build Intelligent Systems

Hands-On Machine Learning with Scikit-Learn and PyTorch: Concepts, Tools, and Techniques to Build Intelligent Systems

BUY & SAVE
$74.99 $76.99
Save 3%
Hands-On Machine Learning with Scikit-Learn and PyTorch: Concepts, Tools, and Techniques to Build Intelligent Systems
4 Ultimate Python Libraries for Data Analysis and Visualization: Leverage Pandas, NumPy, Matplotlib, Seaborn, Julius AI and No-Code Tools for Data ... and Statistical Analysis (English Edition)

Ultimate Python Libraries for Data Analysis and Visualization: Leverage Pandas, NumPy, Matplotlib, Seaborn, Julius AI and No-Code Tools for Data ... and Statistical Analysis (English Edition)

BUY & SAVE
$16.99 $17.95
Save 5%
Ultimate Python Libraries for Data Analysis and Visualization: Leverage Pandas, NumPy, Matplotlib, Seaborn, Julius AI and No-Code Tools for Data ... and Statistical Analysis (English Edition)
5 Data Science Essentials with Scikit-Learn, Pandas, and Matplotlib: A Beginner’s Guide to Modern Analysis and Machine Learning

Data Science Essentials with Scikit-Learn, Pandas, and Matplotlib: A Beginner’s Guide to Modern Analysis and Machine Learning

BUY & SAVE
$7.89 $24.98
Save 68%
Data Science Essentials with Scikit-Learn, Pandas, and Matplotlib: A Beginner’s Guide to Modern Analysis and Machine Learning
6 50 Days of Data Analysis with Python: The Ultimate Challenges Book for Beginners.: Hands-on Challenges with pandas, NumPy, Matplotlib, Sklearn and Seaborn

50 Days of Data Analysis with Python: The Ultimate Challenges Book for Beginners.: Hands-on Challenges with pandas, NumPy, Matplotlib, Sklearn and Seaborn

BUY & SAVE
$30.99
50 Days of Data Analysis with Python: The Ultimate Challenges Book for Beginners.: Hands-on Challenges with pandas, NumPy, Matplotlib, Sklearn and Seaborn
+
ONE MORE?

To display a legend with Matplotlib, you can use the plt.legend() function after plotting your data. This function takes an optional labels parameter where you can specify the names of the labels you want to display in the legend. You can also customize the location of the legend using the loc parameter, which takes values such as "upper right", "lower left", "center", etc. Additionally, you can adjust the size and styling of the legend using parameters like fontsize, title, and shadow. Legends are useful for identifying different elements within a plot, such as lines, markers, or colors, and can enhance the understanding of your data visualizations.

How to add a legend without labels in matplotlib?

You can add a legend without labels in matplotlib by creating a custom legend handler function and specifying it when creating the legend. Here is an example code snippet to do this:

import matplotlib.pyplot as plt import matplotlib.lines as mlines

Create a custom legend handler function

def no_label_legend(color, linestyle): line = mlines.Line2D([], [], color=color, linestyle=linestyle) return line

Plot some data

plt.plot([1, 2, 3], label='Line 1', color='blue') plt.plot([3, 2, 1], label='Line 2', color='red', linestyle='dashed')

Add legend without labels

plt.legend(handles=[no_label_legend('blue', '-'), no_label_legend('red', '--')], loc='upper left')

plt.show()

In this code snippet, the no_label_legend function creates a custom legend handler that simply creates a colored line with the specified color and linestyle. This function is then used when creating the legend by passing the desired color and linestyle for each line. This will create a legend with colored lines but without any labels.

How to hide a legend in matplotlib?

To hide the legend in a matplotlib plot, you can simply call the legend() function with no arguments. Here's an example:

import matplotlib.pyplot as plt

Create some data

x = [1, 2, 3, 4, 5] y = [10, 15, 13, 18, 16]

Plot the data

plt.plot(x, y)

Hide the legend

plt.legend()

plt.show()

This will plot the data without displaying a legend.

How to create a custom legend handler in matplotlib?

To create a custom legend handler in matplotlib, you can use the HandlerBase class to define a new handler function. Here is an example of how you can create a custom legend handler for a scatter plot:

import matplotlib.pyplot as plt from matplotlib.legend_handler import HandlerBase

class CustomLegendHandler(HandlerBase): def create_artists(self, legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans): size = orig_handle.get_sizes()[0] marker = orig_handle.get_paths()[0] handle = plt.Line2D([0],[0], marker=marker, markersize=size, color='black') return [handle]

Create a scatter plot

x = [1, 2, 3, 4, 5] y = [2, 3, 4, 5, 6] sizes = [20, 40, 60, 80, 100] paths = [plt.Circle((0, 0), s) for s in sizes]

fig, ax = plt.subplots() sc = ax.scatter(x, y, s=sizes, marker=paths)

Create custom legend handler

plt.legend([sc], ['Data'], handler_map={type(sc): CustomLegendHandler()})

plt.show()

In this example, we create a custom legend handler CustomLegendHandler that creates a Line2D object with the same marker shape and size as the original scatter plot. We then use this custom legend handler when creating the legend for the scatter plot.

You can customize the create_artists method in the CustomLegendHandler class to create different types of artists for your legend, depending on your specific requirements.