BAR
BAR

Reputation: 17111

Color Individual Lines with Gadfly

Is there a way to color each dataset?

There is a solution using DataFrames, but what about cases without them?

I tried this, but it has no effect:

using Gadfly

plot(
  layer(x=1:10, y=1:10, Stat.step, Geom.line),
  layer(x=1:10, y=2:11, Stat.step, Geom.line),
  color=["red", "green"]
)

Upvotes: 3

Views: 984

Answers (2)

Tom Breloff
Tom Breloff

Reputation: 1802

Plotting should not be this painful. Here's how you do it in Plots using the Gadfly backend:

using Plots; gadfly(size=(400,300))
plot(rand(10,2), line = ([:red :green], :step))

enter image description here

Upvotes: 6

Colin T Bowers
Colin T Bowers

Reputation: 18560

@GnimucK. comment shows how to do this when you're working interactively. That method runs into a few difficulties though when you want to pass a colour in as an argument to a function. In the general case where I have multiple lines where I want the colors to be chosen at run-time, I have a function that looks a bit like what follows:

using Compose, Gadfly
function my_plot_with_colors{T<:Number}(x::Vector{Vector{T}}, y::Vector{Vector{T}}, colorVec::Vector{ASCIIString})
    !(length(x) == length(y) == length(colorVec)) && error("Length mismatch in inputs")
    layerArr = Array(Vector{Layer}, length(x))
    for k = 1:length(x)
        layerArr[k] = layer(x=x[k], y=y[k], Geom.line, Theme(default_color=parse(Compose.Colorant, colourVec[k])))
    end
    return(plot(layerArr...))
end

where, if length(x) = 3, your input vector colourVec would look something like this: ["red", "green", "blue"].

Upvotes: 2

Related Questions