How to give names to the curves in 1D graph of 2D array?

In my M x N array, the columns represent y values and so plot(arr) will produce N curves, which is what I need. But my question is, how can one give a name to each curve?

My first intuition was to give a vector of the names to the label attribute of plot. See the self-contained example below. But it gives the Stringified version of the vector to each curve.

My second intuition was: because each column of the 2D array represents a curve, the names of the curves should be a row vector . . . This leads to error.

A workaround I’ve found so far is to add curves one by one to the plot in a loop using plot!(), specifying the name of the curve each time.

using Plots
arr = hcat([1,2,4], [-1,-2,-4])
xaxis = 100:100:300
nams = ["positive", "negative"]
plot(xaxis, arr; label=nams)
plot(xaxis, arr; label=nams') # row vector of nams

Since you’re already a bit familiar with it, DimensionalData.jl should do this for you pretty easily:

using Plots
arr = hcat([1,2,4], [-1,-2,-4])
da = DimArray(arr, (X(100:100:300), Y(["positive", "negative"])))

The trick is to use permutedims rather than '. Adjoint is annoyingly recursive so wont work on strings.

plot(xaxis, arr; label=permutedims(nams))
1 Like