I’m plotting a series where I’d like to alternate colors for each point. This works fine via the mc keyword, but I can’t get the legend to indicate the color coding scheme correctly:
using Plots
a = rand(10); b = rand(10);
scatter(a, b, mc = [:navy, :crimson], label = ["odd", "even"])
@dpsanders Of course, if there’s no way to accomplish this via the label keyword, one can write a for loop. But this is a rather inelegant solution, don’t you agree?
using Plots
function plotter()
a = rand(100); b = rand(100);
idx_container = [i:5:100 for i in 1:5]
colors = [:navy, :crimson, :olivedrab, :rebeccapurple, :goldenrod]
labels = ["$i mod 5" for i in 1:5]
fig = plot()
for (i, idx) in enumerate(idx_container)
scatter!(fig, a[idx], b[idx], mc = colors[i], label=labels[i])
end
return fig
end
plotter()
You have to create a one array (or @view) for the 1st, 6th, 11th entries, another for the 2nd, 7th, and 12th, etc. This results in code far less legible than a list of labels, and calls plot more times then seemingly necessary.
@nilshg Removing the comma from the label vector doesn’t work, unfortunately:
plots still regards the data as only one series, labeled “odd.”
Ah fair enough - I have to agree with Plots, you are passing just one series. The best best is then probably reshaping as above or just passing two series: