Coloring Different Areas under a Normal Distribution based on X axis

I am trying to achieve results like those below. I am able to get a normal distribution that’s somewhat broken up with vertical bars:

πœ‡=1500
𝜎=300
plot(Normal(πœ‡,𝜎), title="Normal Distribution N($(πœ‡),$(𝜎))", lw=3, label=["SAT Distribution"])
vline!([1200,1800], label="1 Standard Deviation")
vline!([900, 2100], label="2 Standard Deviations")
vline!([600, 2400], label="3 Standard Deviations")

But I’d really prefer to be able to color the portion under a graph in different colors based on the x axis. Is this possible?

My closest attempt is the following, but it just cuts off the rest of the graph, rather than showing the whole curve:

πœ‡=1500
𝜎=300
using Rmath
percentile = signifChop(pnorm(1800,πœ‡,𝜎)*100,3)
plot(Normal(πœ‡,𝜎), title="Normal Distribution N($(πœ‡),$(𝜎)) to $(percentile) percentile", lw=3, label=["SAT Distribution"], fillrange=0)
xlims!((0,1800))

Here’s a few examples of what I’d like to be able to create:

For the first plot,

let n = Normal(1500,300), (from,x0,to) = (600,1800,2400)
    plot(x->pdf(n,x), from, x0, fillrange=0)
    plot!(x->pdf(n,x), from, to, c="black")
    plot!([x0,x0], [0, pdf(n,x0)], c="black")
end
1 Like

To add on yha solution, you can obtain something similar to the second plot with:

let ΞΌ = 1500,
    Οƒ = 300,
    n = Normal(ΞΌ,Οƒ), 
    (from,x0,x1,x2,x3,x4,x5,to) = ΞΌ .+ (Οƒ .* (-5, -3, -2 , -1, 1, 2, 3, 5))
    plot(x->pdf(n,x), from, to, fillrange=0, c=:red4, legend=false)
    plot!(x->pdf(n,x), x0, x5, fillrange=0, c=:gray80, alpha=0.5)
    plot!(x->pdf(n,x), x1, x4, fillrange=0, c=:gray80, alpha=0.5)
    plot!(x->pdf(n,x), x2, x3, fillrange=0, c=:gray80, alpha=0.5)
    plot!(x->pdf(n,x), from, to, c=:black)
    vline!([ΞΌ], line=:dash)
end

1 Like