130

Ever since upgrading matplotlib I get the following error whenever trying to create a legend:

/usr/lib/pymodules/python2.7/matplotlib/legend.py:610: UserWarning: Legend does not support [<matplotlib.lines.Line2D object at 0x3a30810>]
Use proxy artist instead.

http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist

  warnings.warn("Legend does not support %s\nUse proxy artist instead.\n\nhttp://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist\n" % (str(orig_handle),))
/usr/lib/pymodules/python2.7/matplotlib/legend.py:610: UserWarning: Legend does not support [<matplotlib.lines.Line2D object at 0x3a30990>]
Use proxy artist instead.

http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist

  warnings.warn("Legend does not support %s\nUse proxy artist instead.\n\nhttp://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist\n" % (str(orig_handle),))

This even occurs with a trivial script like this:

import matplotlib.pyplot as plt

a = [1,2,3]
b = [4,5,6]
c = [7,8,9]

plot1 = plt.plot(a,b)
plot2 = plt.plot(a,c)

plt.legend([plot1,plot2],["plot 1", "plot 2"])
plt.show()

I've found the link that the error points me towards pretty useless in diagnosing the source of the error.

5 Answers 5

207

You should add commas:

plot1, = plt.plot(a,b)
plot2, = plt.plot(a,c)

The reason you need the commas is because plt.plot() returns a tuple of line objects, no matter how many are actually created from the command. Without the comma, "plot1" and "plot2" are tuples instead of line objects, making the later call to plt.legend() fail.

The comma implicitly unpacks the results so that instead of a tuple, "plot1" and "plot2" automatically become the first objects within the tuple, i.e. the line objects you actually want.

http://matplotlib.sourceforge.net/users/legend_guide.html#adjusting-the-order-of-legend-items

line, = plot(x,sin(x)) what does comma stand for?

Sign up to request clarification or add additional context in comments.

Comments

35

Use the "label" keyword, like so:

plt.plot(x, y, label='x vs. y')

and then add the legend like so:

plt.legend()

The legend will retain line properties like thickness, colours, etc.

enter image description here

Comments

10

Use handles AKA Proxy artists

import matplotlib.lines as mlines
import matplotlib.pyplot as plt
# defining legend style and data
blue_line = mlines.Line2D([], [], color='blue', label='My Label')
reds_line = mlines.Line2D([], [], color='red', label='My Othes')

plt.legend(handles=[blue_line, reds_line])

plt.show()

Comments

0

I had the similar problem with histogram plot. It's not exactly what the OP has asked, but pasting my finding here for the future visitors of "Legend not working":

This code did NOT work:

fig, ax = plt.subplots(figsize=(10, 6))
sns.histplot(data=df_stats, x="score", hue="sys", weights=df_stats[sname], bins=10, kde=False, multiple="dodge")
ax.legend(title="Systems", labels=[labels[k] for k in sys_names])

After changing to the below code, it started working as expected

fig, _ = plt.subplots(figsize=(10, 6))
ax = sns.histplot(data=df_stats, x="score", hue="sys", weights=df_stats[sname], bins=10, kde=False, multiple="dodge")
ax.legend(title="Systems", labels=[labels[k] for k in sys_names])

What was the issue with legends? Notice where ax object is initialized!

PS, Copilot/ChatGPT couldn't solve my bug either and led me to an hour of frustration.

Comments

-3

use label while plotting graph then only u can use legend. x axis name and y axis name is different than legend name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.