UserWarning: Legend does not support Line2D object

pyplot

#1

When plotting with legends using PyPlot, I got the following error. What may this error be caused by?

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

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

  (str(orig_handle),))

Thanks!


#2

What are you plotting? It’s referring to some Line2D object that it says it can’t support.

I use legends in PyPlot all the time so I know they work and I can’t remember seeing that problem.


#3

Hi Daneel, Thanks for the comments! In fact I came across this message when working on a very simple 1D plot.

using PyPlot

f = plot([-1.0; 1.0], ones(2,1), "b")
hold(true)
g = plot([-2.0; -1.0], [0.0; 2.0], "r")

legend((f, g), (L"f(x)", L"g(x)"), loc=2, fontsize="small")

Am I missing anything? Thanks!!


#4

The plot objects get wrapped in arrays. You can do this

    legend([f[1],g[1]],(L"f(x)", L"g(x)"), loc=2, fontsize="small")

or just put the labels in the plot calls (my preference):

    f = plot([-1.0; 1.0], ones(2,1), "b", label=L"f(x)")
    hold(true)
    g = plot([-2.0; -1.0], [0.0; 2.0], "r", label=L"g(x)")
    legend(loc=2, fontsize="small")

#5

Hi Ralph_Smith,

I see! Thanks a lot! I didn’t realize that f and g here are arrays. Yes, it’s easier to just include the legends in the plot commands.


#6

Note that you can also just do

legend([L"f(x)", L"g(x)"], loc=2, fontsize="small")

and it will automatically apply the labels to the first two plotted curves. There’s generally no need to use the output of the plot function at all.


#7

Steven, Thanks for reminding me that! Well, the example I gave is just a simplified version my actual code. In the actual code I have a dozen of calls of plots and only some of them are referred in the legend. So I need only some of the handles, not all of them. Thanks!!


#8

I’m trying to plot two lines by calling plot only once. But both of the legends are set to be the legend for the second line. Am I missing anything? Thanks!!

using PyPlot

plot([-1.0; 1.0], ones(2,1), "b", label=L"f(x)", [-2.0; -1.0], [0.0; 2.0], "r", label=L"g(x)")
legend(loc=2, fontsize="small")


#9

Why don’t you just separate them into 2 different plot commands (which is, in any case, more readable and more extensible to N commands)?


#10

On the one hand, I want to include the julia code into my paper and the space I have is rather limited. The fewer lines, the better. On the other hand, I’m expecting what I have above to work normally. Since what I see is not what I expect, I thought I’d better ask. There might be some ideas of pyplot that I don’t know. I know this is somewhat a silly question. But my curiosity drove me to ask… Sorry…


#11

It’s a fair question, I have just personally never liked that compressed style, but if space is at a premium then it makes sense. I’m afraid I don’t know the answer, though!