How do you color (x,y) scatter plots according to values in z using Plots.jl?
The following method will be much better than jverzani's (you don't want to create a new series for every data point). Plots could use some additional love for manually defining color vectors, but right now gradients are pretty well supported, so you can take advantage of that.
using Plots
pyplot(size=(400,200), legend=false) # set backend and set some session defaults
scatter(rand(30),
m = ColorGradient([:red, :green, :blue]), # colors are defined by a gradient
zcolor = repeat( [0,0.5,1], 10) # sample from the gradient, cycling through: 0, 0.5, 1
)
I would have thought if you defined k
as a vector of color symbols this would work: scatter(x, y, markercolors=k)
, but it doesn't seem to. However, adding them one at a time will, as this example shows:
using Plots
xs = rand(10)
ys = rand(10)
ks = randbool(10) + 1 # 1 or 2
mcols = [:red, :blue] # together, mcols[ks] is the `k` in the question
p = scatter(xs[ks .== 1], ys[ks .== 1], markercolor=mcols[1])
for k = 2:length(mcols)
scatter!(xs[ks .== k], ys[ks .== k], markercolor=mcols[k])
end
p