Reputation: 16906
I'm using Gadfly to plot data in Julia. I have x = an array of floats, and several y1, y2, y3 ... of matching length. How to I plot all the points (x,y1) in green, (x,y2) in red, etc. in one Gadfly plot?
Upvotes: 4
Views: 3673
Reputation: 1802
This sort of stuff is simple with my package https://github.com/tbreloff/Plots.jl:
julia> using Plots; scatter(rand(10,3), c=[:green,:red,:blue])
Upvotes: 0
Reputation: 32351
You can put the data in a DataFrame, with three columns, x
, y
and group
, and use the group as a colour aesthetic.
# Sample data
n = 10
x = collect(1:n)
y1 = rand(n)
y2 = rand(n)
y3 = rand(n)
# Put the data in a DataFrame
using DataFrames
d = DataFrame(
x = vcat(x,x,x),
y = vcat(y1,y2,y3),
group = vcat( rep("1",n), rep("2",n), rep("3",n) )
)
# Plot
using Gadfly
plot(
d,
x=:x, y=:y, color=:group,
Geom.point,
Scale.discrete_color_manual("green","red","blue")
)
As suggested in the comments, you could also use layers:
plot(
layer(x=x, y=y1, Geom.point, Theme(default_color=color("green"))),
layer(x=x, y=y2, Geom.point, Theme(default_color=color("red"))),
layer(x=x, y=y3, Geom.point, Theme(default_color=color("blue")))
)
Upvotes: 7