How to set colours for groups of elements on a plot

I would like to plot the solution for a differential equation, but have certain solutions a specific colour
I am using Plot.jl and it currently looks like this

I would like to specify that u1(t) to u2(t) are brown, u3(t) to u17(t) are green, and u18(t) to u42(t) are red.

This seems like there should be a simple solution but I cannot find anything in the Plot.jl manual.

Attributes http://docs.juliaplots.org/latest/attributes/ will help.

When drawing the series, set linecolor to a vector of the colors you want.

e.g. plot(rand(100,2), linecolor=[:red, :blue])

update

I’ve figured out how to set colours for certain elements on a plot, but now I don’t know how to make a legend based on these groupings. I am sure I have not done it in the most efficient way, but it works more or less (I don’t know how to make the Target Species line be bolder than others [blue]).

Now I would like to add a legend that shows what category each colour means. I know I need to group things together.

What I thought about trying is this, but I get an error

plot(sol2, tspan=(0.0,2000.0), title = "Biomass through time", xlabel = "Time",;
vars = (Nutrients, Plants, Animals, Target_Animals), linecolor=[:brown, :green, :red, :blue], ylabel = "Biomass", lw=0.3)

MethodError: no method matching u_n(::DiffEqArray{Float64,2,Array{Array{Float64,1},1},Array{Float64,1}}, ::Float64...

My attempt at a minimum working example of I create my array and how I would expect to group it (except I don’t know how to make a minimal example using a differential equation)

num_nutrients = 2
num_plant_species = 15
num_animal_species = 25
target_species = 42

total_species = zeros(num_nutrient+num_plant_species+num_animal_species)

function create_bioS_test(total_species, num_nutrients, num_plant_species, num_animal_species)
    bioS_test = zeros(length(total_species))
    bioS_test[1:num_nutrients] = rand(Uniform(0,1), num_nutrients)
    bioS_test[num_nutrients+1:(num_nutrients+num_plant_species)] = rand(Uniform(0,10), num_plant_species) 
    bioS_test[(num_nutrients+num_plant_species+1):end] = rand(Uniform(0,10), num_animal_species)
    return bioS_test
end

bioS_test = create_bioS_test(total_species, num_nutrients, num_plant_species, num_animal_species)

Nutrients = bioS_test[1:num_nutrients]
Plants = bioS_test[num_nutrients+1:(num_nutrients+num_plant_species)]
Animals = bioS_test[(num_nutrients+num_plant_species+1):end]
Target_Animals = bioS_test[(target_species)]

How I made the graph:

plot(sol2, vars =[1,num_nutrients], linecolor=[:brown], tspan=(0.0,1000.0),;
xlabel = "Time" ,ylabel = "Biomass", lw=0.3)
plot!(sol2, vars = ((num_nutrients+1:(num_nutrients+num_plant_species)), linecolor=[:green], lw=0.3)
plot!(sol2, vars = (((num_nutrients+num_plant_species+1):end), linecolor=[:red], lw=0.3) 
plot!(sol2, vars = (target_species), linecolor=[:blue], title = "Biomasses through time", legend=false, lw=0.4)

Any help is appreciated.

Your MWE uses sol2, which seems to be undefined, so I have to guess a little. Have you tried the “primary” or “group” attributes?

I haven’t. I can try but I don’t understand how from the Plot Attributes page. Would I use it like

group = [
(Nutrients = bioS_test[1:num_nutrients]),
(Plants = bioS_test[num_nutrients+1:(num_nutrients+num_plant_species)]),
(Animals = bioS_test[(num_nutrients+num_plant_species+1):end]),
(Target_Animals = bioS_test[(target_species)]),
]

And then

plot(sol2, tspan=(0.0,2000.0), title = "Biomass through time", xlabel = "Time",;
vars = (group), linecolor=[:brown, :green, :red, :blue], ylabel = "Biomass", lw=0.3)

The following uses just two colors and creates two legend entries:

t = LinRange(0,5,500)
plot(t,sin.(t))
plot!(t,cos.(t),primary=false)
plot!(t,@. exp(t-5))