Matplotlib: No artists with labels found to put in legend

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
5 min

banner

# Table of Contents

  1. Matplotlib: No artists with labels found to put in legend
  2. Calling the plt.legend() or ax.legend() methods multiple times
  3. Try using ax.legend() instead of plt.legend()
  4. Trying to place a legend on the axes before creating a plot

# Matplotlib: No artists with labels found to put in legend

The Matplotlib warning "No artists with labels found to put in legend" is shown for multiple reasons:

  1. Not specifying labels at artist creation time or by calling set_label().
  2. Trying to place a legend on the axes before creating a plot.
  3. Calling the plt.legend() or ax.legend() methods multiple times. For example, trying to set the font size before having created the legend.

Here is an example of when the warning is shown.

main.py
import pandas as pd import matplotlib.pyplot as plt test_data = pd.DataFrame.from_dict({ 'x': {0: 9.49084, 1: 14.47866, 2: 5.06412, 3: 11.27358, 4: 5.33439}, 'y': {0: 1.35503, 1: 9.01758, 2: 7.36345, 3: 7.36519, 4: 6.73471} }) fig = plt.figure() ax = fig.add_subplot(221) data = plt.scatter(test_data.x, test_data.y) # ⛔️ No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument. ax.legend() plt.show()

no artists with labels found to put in legend

The Matplotlib Artist is the object that knows how to use a renderer to paint onto the canvas.

The warning is shown because we didn't specify the handles and labels.

One way to resolve the issue is to pass the handles and labels as arguments to the axes.legend() method.

main.py
import pandas as pd import matplotlib.pyplot as plt test_data = pd.DataFrame.from_dict({ 'x': {0: 9.49084, 1: 14.47866, 2: 5.06412, 3: 11.27358, 4: 5.33439}, 'y': {0: 1.35503, 1: 9.01758, 2: 7.36345, 3: 7.36519, 4: 6.73471} }) fig = plt.figure() ax = fig.add_subplot(221) data = plt.scatter(test_data.x, test_data.y) ax.legend([data], ['dots']) plt.show()

pass handles and labels when calling axes legend

The code for this article is available on GitHub

The first argument we passed to the axes.legend() method is a list containing the handles and the second is a list containing the labels.

You can also pass a list containing the labels as an argument to axes.legend().

main.py
import pandas as pd import matplotlib.pyplot as plt test_data = pd.DataFrame.from_dict({ 'x': {0: 9.49084, 1: 14.47866, 2: 5.06412, 3: 11.27358, 4: 5.33439}, 'y': {0: 1.35503, 1: 9.01758, 2: 7.36345, 3: 7.36519, 4: 6.73471} }) fig = plt.figure() ax = fig.add_subplot(221) data = plt.scatter(test_data.x, test_data.y) ax.legend(['dots']) plt.show()

pass list containing the labels to axes legend

The code for this article is available on GitHub

You can also use the Artist.set_label() method to set a label that will be displayed in the legend.

main.py
import pandas as pd import matplotlib.pyplot as plt test_data = pd.DataFrame.from_dict({ 'x': {0: 9.49084, 1: 14.47866, 2: 5.06412, 3: 11.27358, 4: 5.33439}, 'y': {0: 1.35503, 1: 9.01758, 2: 7.36345, 3: 7.36519, 4: 6.73471} }) fig = plt.figure() ax = fig.add_subplot(221) data = plt.scatter(test_data.x, test_data.y) data.set_label('dots') ax.legend() plt.show()

using set label method to resolve the warning

You can also specify the labels at artist creation time, e.g. when calling ax.plot() (or plt.scatter() in the example).

main.py
import pandas as pd import matplotlib.pyplot as plt test_data = pd.DataFrame.from_dict({ 'x': {0: 9.49084, 1: 14.47866, 2: 5.06412, 3: 11.27358, 4: 5.33439}, 'y': {0: 1.35503, 1: 9.01758, 2: 7.36345, 3: 7.36519, 4: 6.73471} }) fig = plt.figure() ax = fig.add_subplot(221) data = plt.scatter(test_data.x, test_data.y, label='dots') ax.legend() plt.show()
The code for this article is available on GitHub

Notice that we passed the label keyword argument when calling plt.scatter().

Matplotlib tries to determine the elements that are to be added to the legend automatically when you don't explicitly pass extra arguments.

However, in some cases, you have to explicitly set the labels.

You can either do this by setting the label keyword argument.

main.py
ax.plot([1, 2, 3], label='Inline label') ax.legend()

Or by using the set_label method.

main.py
line, = ax.plot([1, 2, 3]) line.set_label('Label via method') ax.legend()

Make sure to set the label before calling ax.legend(), otherwise, the warning is shown.

# Calling the plt.legend() or ax.legend() methods multiple times

You will also get the warning if you call the plt.legend() or ax.legend() methods multiple times.

main.py
import pandas as pd import matplotlib.pyplot as plt test_data = pd.DataFrame.from_dict({ 'x': {0: 9.49084, 1: 14.47866, 2: 5.06412, 3: 11.27358, 4: 5.33439}, 'y': {0: 1.35503, 1: 9.01758, 2: 7.36345, 3: 7.36519, 4: 6.73471} }) fig = plt.figure() ax = fig.add_subplot(221) data = plt.scatter(test_data.x, test_data.y) ax.legend(fontsize='x-large') # ⛔️ No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument. ax.legend(['dots']) plt.show()

calling ax legend method multiple times

Notice that we called the ax.legend() method multiple times.

To resolve the issue in this case, pass the fontsize keyword argument when placing the legend on the axes, in a single call.

main.py
import pandas as pd import matplotlib.pyplot as plt test_data = pd.DataFrame.from_dict({ 'x': {0: 9.49084, 1: 14.47866, 2: 5.06412, 3: 11.27358, 4: 5.33439}, 'y': {0: 1.35503, 1: 9.01758, 2: 7.36345, 3: 7.36519, 4: 6.73471} }) fig = plt.figure() ax = fig.add_subplot(221) data = plt.scatter(test_data.x, test_data.y) ax.legend(['dots'], fontsize='x-large') plt.show()

only call ax legend method once

The code for this article is available on GitHub

Now we only call the ax.legend() method once, after creating the plot and the warning is no longer shown.

# Try using ax.legend() instead of plt.legend()

If the issue persists and you use plt.legend(), try to use ax.legend() instead.

In other words, replace this:

main.py
plt.legend()

With this:

main.py
ax.legend()

Using the legend() method on the ax object often resolves the issue.

# Trying to place a legend on the axes before creating a plot

You will also get the warning if you try to place a legend on the axes before creating a plot.

Here is an example.

main.py
import pandas as pd import matplotlib.pyplot as plt test_data = pd.DataFrame.from_dict({ 'x': {0: 9.49084, 1: 14.47866, 2: 5.06412, 3: 11.27358, 4: 5.33439}, 'y': {0: 1.35503, 1: 9.01758, 2: 7.36345, 3: 7.36519, 4: 6.73471} }) # ⛔️ No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument. plt.legend() fig = plt.figure() ax = fig.add_subplot(221) data = plt.scatter(test_data.x, test_data.y, label='dots') plt.show()

trying to place legend on axes before creating plot

Notice that we called the plt.legend() method too early.

Make sure to call the method after you create a plot to resolve the issue.

main.py
import pandas as pd import matplotlib.pyplot as plt test_data = pd.DataFrame.from_dict({ 'x': {0: 9.49084, 1: 14.47866, 2: 5.06412, 3: 11.27358, 4: 5.33439}, 'y': {0: 1.35503, 1: 9.01758, 2: 7.36345, 3: 7.36519, 4: 6.73471} }) fig = plt.figure() ax = fig.add_subplot(221) data = plt.scatter(test_data.x, test_data.y, label='dots') # ✅ Move method call here plt.legend() plt.show()

call legend method after creating plot

The code for this article is available on GitHub

After moving the plt.legend() method call to the bottom, the issue is resolved.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.