# Julia PyPlot add legend

**URL:** <https://discourse.julialang.org/t/julia-pyplot-add-legend/27616>\
**Category:** New to Julia\
**Created:** [August 16, 2019, 1:11pm UTC](https://discourse.julialang.org/t/julia-pyplot-add-legend/27616 "2019-08-16T13:11:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [August 16, 2019, 1:11pm UTC](https://discourse.julialang.org/t/julia-pyplot-add-legend/27616/1 "2019-08-16T13:11:14Z")

</div>

hello,  
is there a simple way to add a legend to the plot with PyPlot? There are different solutions online but none worked.  
I have this command:

```julia
myPlot = plot(x, y1, x, y2, linewidth=2.0)
legend()
savefig("./plot.png", dpi = 300, format = "png", transparent = false)

```

where y1 and y2 are two vectors sharing the same x values. But `legend()` does not provide ` **1. Automatic detection of elements to be shown in the legend** `: it only produces a small square in the top right corner. likewise `ax.legend` or `legend(loc="upper right")`.  
Thank you

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [August 17, 2019, 12:49pm UTC](https://discourse.julialang.org/t/julia-pyplot-add-legend/27616/2 "2019-08-17T12:49:12Z")

</div>

Aren’t you supposed to label your plots? Maybe split the plots into two and add individual labels as `label="whatever you want"` in each plot.

---

<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:** [August 17, 2019, 1:01pm UTC](https://discourse.julialang.org/t/julia-pyplot-add-legend/27616/3 "2019-08-17T13:01:53Z")

</div>

> [@Luigi\_Marongiu](#):
>
> But `legend()` does not provide …

As explained in the [docs](https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.pyplot.legend.html), `legend()` only works if you pass `label=` to `plot` or explicitly call `set_label`. Otherwise, you need to pass the desired legends to `legend`, e.g.:

```julia
legend(["first line", "second line"])

```

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [August 17, 2019, 1:10pm UTC](https://discourse.julialang.org/t/julia-pyplot-add-legend/27616/4 "2019-08-17T13:10:59Z")

</div>

Thank you, `legend(["first line", "second line"])` worked. And yes, I am using Pyplot. But I did not understand the syntax to place the label directly in the plot call:

```julia
julia> myPlot = plot(x, y1, x, y2, linewidth=2.0, label='Inline label')
ERROR: syntax: invalid character literal

```

If I do

```julia
myPlot = plot(x, y1, x, y2, linewidth=2.0, label=["first line", "second line"])

```

the label is gone

---

<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:** [August 17, 2019, 1:44pm UTC](https://discourse.julialang.org/t/julia-pyplot-add-legend/27616/5 "2019-08-17T13:44:31Z")

</div>

> [@Luigi\_Marongiu](#):
>
> ```julia
> julia> myPlot = plot(x, y1, x, y2, linewidth=2.0, label='Inline label')
> ERROR: syntax: invalid character literal
> 
> ```

In Python, a string can be either double- or single-quoted (`"foo"` or `'foo'`), for reasons I don’t comprehend. In Julia, strings _must_ [use double quotes](https://pkg.julialang.org/docs/julia/THl1k/1.1.1/manual/strings.html#String-Basics-1). Single quotes are used for literals `'x'` of the [character type](https://pkg.julialang.org/docs/julia/THl1k/1.1.1/manual/strings.html#man-characters-1) `Char` (Python [doesn’t have](https://stackoverflow.com/questions/47310929/does-python-support-character-type) a character type).
