"No artists with labels found to put in legend." error when changing the legend size in pyplot
Asked Answered
P

2

19

I want to make my legend size bigger in Pyplot. I used this answer to do that. Here is my code.

import matplotlib.pyplot as plt
import seaborn as sns

sns.set_style("whitegrid")
plt.rcParams['figure.figsize'] = [15, 7]

lst = [1,2,3,4,5,6,7,8,9,8,7,6,5,4,3,4,5,6]

plt.plot(lst)
plt.legend(fontsize="x-large") # Here I make it bigger but doesn't work
plt.legend(["This is my legend"])
plt.ylabel('some numbers')
plt.show()

I get this warning and I don't know what is wrong. I don't understand what "Artist" means here.

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.

Promulgate answered 29/11, 2022 at 1:33 Comment(0)
P
16

"Artist" is a term from matplotlib:

https://matplotlib.org/stable/tutorials/intermediate/artists.html

Presumably the error message means that there are no items in the legend whose font size can be changed.

Maybe pass the fontsize argument to the same plt.legend call in which you create the legend, or call plt.legend(fontsize=...) after you have created the legend.

Pauwles answered 29/11, 2022 at 1:42 Comment(1)
plt.legend(["This is my legend"], fontsize="x-large") This worked.Promulgate
L
4

This warning is saying that legend was attempted to be created without any labels to show as a legend. The solution is to pass the label in the legend() call as the OP observed:

import matplotlib.pyplot as plt
lst = [1, 3, 2]

plt.plot(lst)
plt.legend(["This is my legend"], fontsize="x-large")

or pass the label in the plot call and draw the legend later.

plt.plot(lst, label="This is my legend")  # <--- the label corresponds to this plot
plt.legend(fontsize="x-large")            # <--- the above plot's label is drawn in the legend
Length answered 7/1 at 20:31 Comment(1)
Tkx, it works for me.Melo

© 2022 - 2024 — McMap. All rights reserved.