问题
Using the Plots.jl package in Julia, I am able to use various backends to make a scatter plot based on two vectors x
and y
k = 100
x = rand(k)
y = rand(k)
scatter(x, y)
I am unable to find information about how to color them according to some length k
vector z
. How do you do that?
回答1:
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
)
回答2:
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
来源:https://stackoverflow.com/questions/35130093/how-do-you-color-x-y-scatter-plots-according-to-values-in-z-using-plots-jl