Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you color (x,y) scatter plots according to values in z using Plots.jl?

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?

like image 445
pkofod Avatar asked Feb 01 '16 12:02

pkofod


3 Answers

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
       )

enter image description here

like image 190
Tom Breloff Avatar answered Jan 02 '23 23:01

Tom Breloff


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
like image 21
jverzani Avatar answered Jan 02 '23 22:01

jverzani


If the elements in vector z are categorical rather than continuous values, you might want to consider using the group parameter to the plotting call as follows:

using Plots

# visualize x and y colouring points based on category z
scatter(x, y, group=z)
like image 39
James Bowman Avatar answered Jan 03 '23 00:01

James Bowman