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.
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.
Hi Daneel, Thanks for the comments! In fact I came across this message when working on a very simple 1D plot.
f = plot([-1.0; 1.0], ones(2,1), "b")
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!!
The plot objects get wrapped in arrays. You can do this
legend([f,g],(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)")
g = plot([-2.0; -1.0], [0.0; 2.0], "r", label=L"g(x)")
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.
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.
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!!
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!!
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)")
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)?
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…
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!
The array index needed to be “0” instead of “1” in my scenario when working around the same issue.
#plt.figure(figsize=(6, 6), dpi=80)
act_plt = plt.plot(x_data,y,’.’,color=‘green’)
l1_plt = plt.plot(x_data,y_pred_l1,’.’,color=‘blue’)
l2_plt = plt.plot(x_data,y_pred_l2,’.’,color=‘purple’)
ax = plt.gca()
ax.legend([act_plt, l1_plt, l2_plt],[‘Actual’,‘L1’,‘L2’])