# How to make plot legend have names instead of "y1, y2, ..., yn"

**URL:** <https://discourse.julialang.org/t/how-to-make-plot-legend-have-names-instead-of-y1-y2-yn/43270>\
**Category:** General Usage\
**Tags:** plots\
**Created:** [July 18, 2020, 2:29am UTC](https://discourse.julialang.org/t/how-to-make-plot-legend-have-names-instead-of-y1-y2-yn/43270 "2020-07-18T02:29:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkarikom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkarikom/32/7096_2.png) [@mkarikom](https://discourse.julialang.org/u/mkarikom)\
**Post date:** [July 18, 2020, 2:29am UTC](https://discourse.julialang.org/t/how-to-make-plot-legend-have-names-instead-of-y1-y2-yn/43270/1 "2020-07-18T02:29:53Z")

</div>

Much to my chagrin, after reading the [Plots.jl Tutorial](https://docs.juliaplots.org/latest/tutorial/) in its entirety I can’t get this to work:

```
histogram([[1,2,3,1],[1,2,2,2,2]],labels=["foo","bar"])

```

Gives (obviously I don’t want arrays for variable names):  
 ![hist1](https://global.discourse-cdn.com/julialang/original/3X/1/a/1a2efbaac9acad944e51c63593abf5883aec49b8.png)

Maybe I just need to name the input variables? Unfortunately the following doesn’t work either:

```
foo = [1,2,3,1]; bar = [1,2,2,2,2]
histogram([foo,bar])

```

Gives:  
 ![hist1](https://global.discourse-cdn.com/julialang/original/3X/8/b/8bb0b28bef955725894e1f29210faa717e4e75da.png)

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [July 18, 2020, 4:51am UTC](https://discourse.julialang.org/t/how-to-make-plot-legend-have-names-instead-of-y1-y2-yn/43270/2 "2020-07-18T04:51:13Z")

</div>

Make the vector of labels a row vector instead of a standard column vector.

---

<div class="post-metadata">

**Author:** ![mkarikom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkarikom/32/7096_2.png) [@mkarikom](https://discourse.julialang.org/u/mkarikom)\
**Post date:** [July 18, 2020, 3:14pm UTC](https://discourse.julialang.org/t/how-to-make-plot-legend-have-names-instead-of-y1-y2-yn/43270/3 "2020-07-18T15:14:31Z")

</div>

Thanks @baggepinnen. That seems logical given the result of the first command, but the following commands give the same plot:

```
histogram([[1,2,3,1],[1,2,2,2,2]],labels=["foo";"bar"])
histogram([[1,2,3,1],[1,2,2,2,2]],labels=["foo","bar"])

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [July 18, 2020, 3:28pm UTC](https://discourse.julialang.org/t/how-to-make-plot-legend-have-names-instead-of-y1-y2-yn/43270/4 "2020-07-18T15:28:01Z")

</div>

None of those are row vectors, try space instead of comma or semicolon.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [July 18, 2020, 3:28pm UTC](https://discourse.julialang.org/t/how-to-make-plot-legend-have-names-instead-of-y1-y2-yn/43270/5 "2020-07-18T15:28:24Z")

</div>

The semicolon still makes a vector, whereas you need a 1-row matrix:

```julia
julia> ["foo";"bar"] # vcat("foo", "bar"), which agrees with ["foo","bar"]
2-element Array{String,1}:
 "foo"
 "bar"

julia> ["foo" "bar"] # hcat("foo", "bar")
1×2 Array{String,2}:
 "foo" "bar"

```

You could also write `permutedims(["foo", "bar"])` if you already had the vector.

> [@mkarikom](#):
>
> `histogram([foo,bar])`

Note BTW that the reason this can’t work is that Julia doesn’t have non-standard evaluation. The function `histogram` has no way of seeing what variable names were used, all it gets (in this case) is a vector of vectors of numbers, after everything inside its `()` has been worked out.

---

<div class="post-metadata">

**Author:** ![mkarikom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkarikom/32/7096_2.png) [@mkarikom](https://discourse.julialang.org/u/mkarikom)\
**Post date:** [July 19, 2020, 12:28am UTC](https://discourse.julialang.org/t/how-to-make-plot-legend-have-names-instead-of-y1-y2-yn/43270/6 "2020-07-19T00:28:42Z")

</div>

perfect, thanks

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [July 19, 2020, 12:53am UTC](https://discourse.julialang.org/t/how-to-make-plot-legend-have-names-instead-of-y1-y2-yn/43270/7 "2020-07-19T00:53:04Z")

</div>

Does anyone know why this design was chosen? It’s far more natural to write an array of labels with commas than to write a row matrix.

---

<div class="post-metadata">

**Author:** ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)\
**Post date:** [July 20, 2020, 5:27pm UTC](https://discourse.julialang.org/t/how-to-make-plot-legend-have-names-instead-of-y1-y2-yn/43270/8 "2020-07-20T17:27:31Z")

</div>

It seems to be consistent with line plots, where each column of a 2d array is a series. So it would make sense for each label to be a column. From the [documentation](https://docs.juliaplots.org/latest/tutorial/#Plot-Attributes-1):

```julia
x = 1:10; y = rand(10, 2) # 2 columns means two lines
plot(x, y, title = "Two Lines", label = ["Line 1" "Line 2"], lw = 3)

```

Histograms get converted into x-y data under the hood, making it non-obvious what the `labels` correspond to. But we can guess each resulting column is still a series, so the labels work in columns as well.
