# UserWarning: Legend does not support Line2D object

**URL:** https://discourse.julialang.org/t/userwarning-legend-does-not-support-line2d-object/970
**Category:** Visualization
**Tags:** pyplot
**Created:** [December 15, 2016, 4:01pm UTC](https://discourse.julialang.org/t/userwarning-legend-does-not-support-line2d-object/970 "2016-12-15T16:01:51Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)
#### Post date: [December 15, 2016, 4:01pm UTC](https://discourse.julialang.org/t/userwarning-legend-does-not-support-line2d-object/970/1 "2016-12-15T16:01:51Z")

</div>

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

```julia
/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!

---

<div class="post-metadata">

### Author: ![Daneel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daneel/32/175_2.png) [@Daneel](https://discourse.julialang.org/u/Daneel)
#### Post date: [December 16, 2016, 6:54am UTC](https://discourse.julialang.org/t/userwarning-legend-does-not-support-line2d-object/970/2 "2016-12-16T06:54:37Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)
#### Post date: [December 17, 2016, 3:18pm UTC](https://discourse.julialang.org/t/userwarning-legend-does-not-support-line2d-object/970/3 "2016-12-17T15:18:22Z")

</div>

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

```julia
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!!

---

<div class="post-metadata">

### Author: ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)
#### Post date: [December 17, 2016, 3:55pm UTC](https://discourse.julialang.org/t/userwarning-legend-does-not-support-line2d-object/970/4 "2016-12-17T15:55:42Z")

</div>

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

```julia
    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):

```julia
    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")

```

---

<div class="post-metadata">

### Author: ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)
#### Post date: [December 17, 2016, 4:05pm UTC](https://discourse.julialang.org/t/userwarning-legend-does-not-support-line2d-object/970/5 "2016-12-17T16:05:38Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 17, 2016, 8:15pm UTC](https://discourse.julialang.org/t/userwarning-legend-does-not-support-line2d-object/970/6 "2016-12-17T20:15:06Z")

</div>

Note that you can also just do

```julia
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.

---

<div class="post-metadata">

### Author: ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)
#### Post date: [December 17, 2016, 9:35pm UTC](https://discourse.julialang.org/t/userwarning-legend-does-not-support-line2d-object/970/7 "2016-12-17T21:35:41Z")

</div>

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!!

---

<div class="post-metadata">

### Author: ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)
#### Post date: [December 24, 2016, 5:21pm UTC](https://discourse.julialang.org/t/userwarning-legend-does-not-support-line2d-object/970/8 "2016-12-24T17:21:04Z")

</div>

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!!

```julia
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")

```

 ![](https://global.discourse-cdn.com/julialang/original/3X/c/a/ca2e4b7cec77af234fcaee3f28246e96e1f108b4.png)

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [December 24, 2016, 11:47pm UTC](https://discourse.julialang.org/t/userwarning-legend-does-not-support-line2d-object/970/9 "2016-12-24T23:47:56Z")

</div>

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)?

---

<div class="post-metadata">

### Author: ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)
#### Post date: [December 25, 2016, 9:32am UTC](https://discourse.julialang.org/t/userwarning-legend-does-not-support-line2d-object/970/10 "2016-12-25T09:32:46Z")

</div>

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…

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [December 25, 2016, 10:35am UTC](https://discourse.julialang.org/t/userwarning-legend-does-not-support-line2d-object/970/11 "2016-12-25T10:35:41Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![KALIDAS\_Y](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kalidas_y/32/9321_2.png) [@KALIDAS\_Y](https://discourse.julialang.org/u/KALIDAS_Y)
#### Post date: [July 13, 2019, 10:00pm UTC](https://discourse.julialang.org/t/userwarning-legend-does-not-support-line2d-object/970/12 "2019-07-13T22:00:52Z")

</div>

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[0], l1\_plt[0], l2\_plt[0]],[‘Actual’,‘L1’,‘L2’])

plt.show()
