# Coloring Different Areas under a Normal Distribution based on X axis

**URL:** <https://discourse.julialang.org/t/coloring-different-areas-under-a-normal-distribution-based-on-x-axis/17412>\
**Category:** Visualization\
**Tags:** question\
**Created:** [November 11, 2018, 10:11pm UTC](https://discourse.julialang.org/t/coloring-different-areas-under-a-normal-distribution-based-on-x-axis/17412 "2018-11-11T22:11:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cjkinni](https://avatars.discourse-cdn.com/v4/letter/c/b4bc9f/32.png) [@cjkinni](https://discourse.julialang.org/u/cjkinni)\
**Post date:** [November 11, 2018, 10:11pm UTC](https://discourse.julialang.org/t/coloring-different-areas-under-a-normal-distribution-based-on-x-axis/17412/1 "2018-11-11T22:11:34Z")

</div>

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:

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

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

 ![04%20PM](https://global.discourse-cdn.com/julialang/original/3X/2/1/2104504b669cba770a54c8641eb992cb94946e7b.png)

 ![05%20PM](https://global.discourse-cdn.com/julialang/original/3X/2/8/280116abd94a1cd66618a5fb58406d0ef7c30990.png)

 ![26%20PM](https://global.discourse-cdn.com/julialang/original/3X/6/6/66746b4c8ed001b221a2ba1fdb2b8e4a95a1becf.png)

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [November 11, 2018, 10:49pm UTC](https://discourse.julialang.org/t/coloring-different-areas-under-a-normal-distribution-based-on-x-axis/17412/2 "2018-11-11T22:49:09Z")

</div>

For the first plot,

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

```

---

<div class="post-metadata">

**Author:** ![davide](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davide/32/52136_2.png) [@davide](https://discourse.julialang.org/u/davide)\
**Post date:** [November 12, 2018, 2:31pm UTC](https://discourse.julialang.org/t/coloring-different-areas-under-a-normal-distribution-based-on-x-axis/17412/3 "2018-11-12T14:31:51Z")

</div>

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

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

```
