Plots (plotly) multiple series or line labels in legend

how does one label two lines?

using Plots; pyplot()
plot(Plots.fakedata(50,2), w=3, labels=[ "a", "b" ])

the blue line is not labeled “a” and the red line is not labeled “b” .

PS: Overview · Plots writes “if label is empty”…what is empty?

1 Like

This is the #1 most asked question on Plots. The answer is that columns are series - so you should use a row matrix. label = ["a" "b"].
There’s an open issue to change the behaviour to accept a Vector.

10 Likes

For anyone else landing here 5+ years later, it seems Plots.jl has settled on not letting you pass in a Vector of labels. It must be a row vector as shown above.

Warning: What follows is guidance from a julia novice. If you have labels=["a","b"] of type Vector{String}, then I think the cleanest way to pass it in all at once is

t = 1:100
y = rand(Float64, (100,3))
labels = ["a","b","c"]
plot(t, y, labels = hcat(labels...))

Probably the best method overall is to call plot for each series and set that individual label like this:

t = 1:100
y = rand(Float64, (100,3))
labels = ["a","b","c"]
pl = plot()
for i in axes(y, 2)
    plot!(pl,t,y[:,i], label=labels[i])
end
display(pl)
1 Like

I usually do

labels = ["a","b","c"]
plot(t, y, labels = permutedims(labels))
3 Likes