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

Much to my chagrin, after reading the Plots.jl 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): 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: Make the vector of labels a row vector instead of a standard column vector.

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

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

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

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

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.

perfect, thanks

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.

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:

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