How can I fix the error “no handles with labels found to put in legend” in Python? 2

How can I fix the error “no handles with labels found to put in legend” in Python?

The error “no handles with labels found to put in legend” in Python occurs when the legend function is unable to find any handles (objects that represent the data on the plot) with corresponding labels. This error can be fixed by ensuring that all the plotted data have labels specified and that these labels are correctly passed to the legend function. Additionally, checking for any typos or missing labels in the code can also help resolve this error.

Fix in Python: no handles with labels found to put in legend


One warning you may encounter when using matplotlib is:

No handles with labels found to put in legend.

This warning usually occurs for one of two reasons:

1. You failed to create labels for the data in the plot.

2. You attempted to create a legend before creating a plot.

The following examples shows how to avoid this warning in both scenarios.

Example 1: You failed to create labels for the data in the plot.

Suppose we attempt to use the following code to create a line chart in matplotlib with a legend and labels:

import matplotlib.pyplotas plt
import pandas as pd

#define data values
df = pd.DataFrame({'x': [18, 22, 19, 14, 14, 11, 20, 28],
                   'y': [5, 7, 7, 9, 12, 9, 9, 4],
                   'z': [11, 8, 10, 6, 6, 5, 9, 12]})

#add multiple lines to matplotlib plot
plt.plot(df['x'], color='green')
plt.plot(df['y'], color='blue')
plt.plot(df['z'], color='purple')

#attempt to add legend to plot
plt.legend()

No handles with labels found to put in legend.

Matplotlib creates the line plot, but we receive the warning of No handles with labels found to put in legend.

To avoid this warning, we must use the label argument to provide a label for each line in the plot:

import matplotlib.pyplotas plt
import pandas as pd

#define data values
df = pd.DataFrame({'x': [18, 22, 19, 14, 14, 11, 20, 28],
                   'y': [5, 7, 7, 9, 12, 9, 9, 4],
                   'z': [11, 8, 10, 6, 6, 5, 9, 12]})

#add multiple lines to matplotlib plot
plt.plot(df['x'], label='x', color='green')
plt.plot(df['y'], label='y', color='blue')
plt.plot(df['z'], label='z', color='purple')

#attempt to add legend to plot
plt.legend()

Notice that a legend is created with labels and we don’t receive any warning this time.

Example 2: You attempted to create a legend before creating a plot.

Suppose we attempt to use the following code to create a line chart in matplotlib with a legend and labels:

import matplotlib.pyplotas plt
import pandas as pd

#define data values
df = pd.DataFrame({'x': [18, 22, 19, 14, 14, 11, 20, 28],
                   'y': [5, 7, 7, 9, 12, 9, 9, 4],
                   'z': [11, 8, 10, 6, 6, 5, 9, 12]})

#attempt to add legend to plot
plt.legend()

#add multiple lines to matplotlib plot
plt.plot(df['x'], label='x', color='green')
plt.plot(df['y'], label='y', color='blue')
plt.plot(df['z'], label='z', color='purple')

No handles with labels found to put in legend.

Matplotlib creates the line plot, but we receive the warning of No handles with labels found to put in legend.

To avoid this warning, we must use plt.legend()after adding the lines to the plot:

import matplotlib.pyplotas plt
import pandas as pd

#define data values
df = pd.DataFrame({'x': [18, 22, 19, 14, 14, 11, 20, 28],
                   'y': [5, 7, 7, 9, 12, 9, 9, 4],
                   'z': [11, 8, 10, 6, 6, 5, 9, 12]})

#add multiple lines to matplotlib plot
plt.plot(df['x'], label='x', color='green')
plt.plot(df['y'], label='y', color='blue')
plt.plot(df['z'], label='z', color='purple')

#attempt to add legend to plot
plt.legend()

A legend is created with labels and we don’t receive any warning this time.

Additional Resources

The following tutorials explain how to fix other common errors in Python:

Cite this article

stats writer (2024). How can I fix the error “no handles with labels found to put in legend” in Python?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-fix-the-error-no-handles-with-labels-found-to-put-in-legend-in-python/

stats writer. "How can I fix the error “no handles with labels found to put in legend” in Python?." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-fix-the-error-no-handles-with-labels-found-to-put-in-legend-in-python/.

stats writer. "How can I fix the error “no handles with labels found to put in legend” in Python?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-fix-the-error-no-handles-with-labels-found-to-put-in-legend-in-python/.

stats writer (2024) 'How can I fix the error “no handles with labels found to put in legend” in Python?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-fix-the-error-no-handles-with-labels-found-to-put-in-legend-in-python/.

[1] stats writer, "How can I fix the error “no handles with labels found to put in legend” in Python?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I fix the error “no handles with labels found to put in legend” in Python?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top