Colored legend with Makie

A have a colored scatterplot (made with Makie) and would like to make a legend that explains what the colors mean. How should I modify the following example to achieve this? Tried the kwargs suggested by the docs of legend(), but couldn’t get it to work.

using Makie
set1 = rand(Point3f0, 5)
set2 = rand(Point3f0, 5)
set3 = rand(Point3f0, 5)

sc = Scene()
scatter!(sc, set1, color=:blue)
scatter!(sc, set2, color=:red)
scatter!(sc, set3, color=:green)
texts = ["scene", "set1", "set2", "set3"]
colors = [:black, :blue, :red, :green]
sl = legend(sc, texts, color=colors)
vbox(sc, sl)
1 Like

This should work:

using Makie
set1 = rand(Point3f0, 5)
set2 = rand(Point3f0, 5)
set3 = rand(Point3f0, 5)

sc = Scene()
scatter!(sc, set1, color=:blue)
scatter!(sc, set2, color=:red)
scatter!(sc, set3, color=:green)
texts = ["set1", "set2", "set3"]
colors = [:black, :blue, :red, :green]
plots = sc.plots[2:end] # exclude axis, which is always plot 1
sl = legend(sc.plots[2:end], texts)
vbox(sc, sl)
2 Likes

Thank you!