# How to set colours for groups of elements on a plot

**URL:** <https://discourse.julialang.org/t/how-to-set-colours-for-groups-of-elements-on-a-plot/25050>\
**Category:** New to Julia\
**Created:** [June 7, 2019, 4:14pm UTC](https://discourse.julialang.org/t/how-to-set-colours-for-groups-of-elements-on-a-plot/25050 "2019-06-07T16:14:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [June 7, 2019, 4:14pm UTC](https://discourse.julialang.org/t/how-to-set-colours-for-groups-of-elements-on-a-plot/25050/1 "2019-06-07T16:14:32Z")

</div>

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

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/3/43e2b7f9529b04a2a87410930636161a5549676d.png)

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](http://docs.juliaplots.org/latest/colors/).

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [June 7, 2019, 4:59pm UTC](https://discourse.julialang.org/t/how-to-set-colours-for-groups-of-elements-on-a-plot/25050/2 "2019-06-07T16:59:28Z")

</div>

Attributes [Overview · Plots](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])`

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [November 22, 2019, 3:37pm UTC](https://discourse.julialang.org/t/how-to-set-colours-for-groups-of-elements-on-a-plot/25050/3 "2019-11-22T15:37:54Z")

</div>

_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]).

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/1/31802ed23ae87181f844007d92082980c99699fb.png)

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

```julia
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)

```julia
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:

```julia
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.

---

<div class="post-metadata">

**Author:** ![tobydriscoll](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobydriscoll/32/1843_2.png) [@tobydriscoll](https://discourse.julialang.org/u/tobydriscoll)\
**Post date:** [November 22, 2019, 4:09pm UTC](https://discourse.julialang.org/t/how-to-set-colours-for-groups-of-elements-on-a-plot/25050/4 "2019-11-22T16:09:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [November 22, 2019, 4:17pm UTC](https://discourse.julialang.org/t/how-to-set-colours-for-groups-of-elements-on-a-plot/25050/5 "2019-11-22T16:17:12Z")

</div>

I haven’t. I can try but I don’t understand how from the [Plot Attributes](http://docs.juliaplots.org/latest/attributes/) page. Would I use it like

```julia
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

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![tobydriscoll](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobydriscoll/32/1843_2.png) [@tobydriscoll](https://discourse.julialang.org/u/tobydriscoll)\
**Post date:** [November 22, 2019, 4:30pm UTC](https://discourse.julialang.org/t/how-to-set-colours-for-groups-of-elements-on-a-plot/25050/6 "2019-11-22T16:30:15Z")

</div>

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

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

```
