Skip to main content
infervour.com

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 October 2025

1 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
$37.93 $49.99
Save 24%
Python Data Cleaning Cookbook: Prepare your data for analysis with pandas, NumPy, Matplotlib, scikit-learn, and OpenAI
2 Python Data Science Handbook: Essential Tools for Working with Data

Python Data Science Handbook: Essential Tools for Working with Data

  • COMPREHENSIVE GUIDE TO DATA SCIENCE WITH PRACTICAL PYTHON EXAMPLES.
  • ESSENTIAL TOOLS FOR BEGINNERS AND EXPERTS IN DATA ANALYSIS.
  • ENHANCE YOUR SKILLS WITH REAL-WORLD CASE STUDIES AND EXERCISES.
BUY & SAVE
$71.36
Python Data Science Handbook: Essential Tools for Working with Data
3 Data Science ToolBox for Beginners: Learn Essentials tools like Pandas, Dask, Numpy, Matplotlib, Seaborn, Scikit-learn, Scipy, TensorFlow/Keras, Plotly, and More

Data Science ToolBox for Beginners: Learn Essentials tools like Pandas, Dask, Numpy, Matplotlib, Seaborn, Scikit-learn, Scipy, TensorFlow/Keras, Plotly, and More

BUY & SAVE
$9.99
Data Science ToolBox for Beginners: Learn Essentials tools like Pandas, Dask, Numpy, Matplotlib, Seaborn, Scikit-learn, Scipy, TensorFlow/Keras, Plotly, and More
4 Python and Matplotlib Essentials for Scientists and Engineers (Iop Concise Physics)

Python and Matplotlib Essentials for Scientists and Engineers (Iop Concise Physics)

BUY & SAVE
$23.17 $39.95
Save 42%
Python and Matplotlib Essentials for Scientists and Engineers (Iop Concise Physics)
5 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
$29.99
50 Days of Data Analysis with Python: The Ultimate Challenges Book for Beginners.: Hands-on Challenges with pandas, NumPy, Matplotlib, Sklearn and Seaborn
6 Ultimate Python Libraries for Data Analysis and Visualization: Leverage Pandas, NumPy, Matplotlib, Seaborn, Julius AI and No-Code Tools for Data Acquisition, ... 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 Acquisition, ... and Statistical Analysis (English Edition)

BUY & SAVE
$16.99
Ultimate Python Libraries for Data Analysis and Visualization: Leverage Pandas, NumPy, Matplotlib, Seaborn, Julius AI and No-Code Tools for Data Acquisition, ... and Statistical Analysis (English Edition)
+
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.