# How can I shade an area under a curve between two lines?

**URL:** <https://discourse.julialang.org/t/how-can-i-shade-an-area-under-a-curve-between-two-lines/24897>\
**Category:** Data\
**Created:** [June 3, 2019, 7:52pm UTC](https://discourse.julialang.org/t/how-can-i-shade-an-area-under-a-curve-between-two-lines/24897 "2019-06-03T19:52:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alessandro\_Vivas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alessandro_vivas/32/7389_2.png) [@Alessandro\_Vivas](https://discourse.julialang.org/u/Alessandro_Vivas)\
**Post date:** [June 3, 2019, 7:52pm UTC](https://discourse.julialang.org/t/how-can-i-shade-an-area-under-a-curve-between-two-lines/24897/1 "2019-06-03T19:52:24Z")

</div>

I am plotting a normal curve and I want shaded some areas in this curve. I want to shaded to values minor than -1.96 e greater than 1.96. I tried some options: fill and fill area but I didn’t get sucess.

x = rand( Normal( 0 , 1 ),1000000)  
plot(x,seriestype=:density,label=“x series”)  
vline!([-1.96], line=:dash)  
vline!([1.96], line=:dash)

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [June 3, 2019, 11:22pm UTC](https://discourse.julialang.org/t/how-can-i-shade-an-area-under-a-curve-between-two-lines/24897/2 "2019-06-03T23:22:57Z")

</div>

Try vspan! or hspan! instead of vline!.

---

<div class="post-metadata">

**Author:** ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)\
**Post date:** [June 4, 2019, 5:05am UTC](https://discourse.julialang.org/t/how-can-i-shade-an-area-under-a-curve-between-two-lines/24897/3 "2019-06-04T05:05:59Z")

</div>

[https://github.com/mcreel/Econometrics/blob/master/src/NP/npdensity.jl](https://github.com/mcreel/Econometrics/blob/master/src/NP/npdensity.jl)

will do that, a screenshot of the appearance is at [https://github.com/mcreel/Econometrics](https://github.com/mcreel/Econometrics)

---

<div class="post-metadata">

**Author:** ![Alessandro\_Vivas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alessandro_vivas/32/7389_2.png) [@Alessandro\_Vivas](https://discourse.julialang.org/u/Alessandro_Vivas)\
**Post date:** [June 4, 2019, 6:16pm UTC](https://discourse.julialang.org/t/how-can-i-shade-an-area-under-a-curve-between-two-lines/24897/4 "2019-06-04T18:16:45Z")

</div>

Thank you!! It works!

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [June 6, 2019, 11:24am UTC](https://discourse.julialang.org/t/how-can-i-shade-an-area-under-a-curve-between-two-lines/24897/5 "2019-06-06T11:24:18Z")

</div>

Maybe you could share an example of usage? That would help future readers fine-tune their shading options 👍

---

<div class="post-metadata">

**Author:** ![Alessandro\_Vivas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alessandro_vivas/32/7389_2.png) [@Alessandro\_Vivas](https://discourse.julialang.org/u/Alessandro_Vivas)\
**Post date:** [June 6, 2019, 8:26pm UTC](https://discourse.julialang.org/t/how-can-i-shade-an-area-under-a-curve-between-two-lines/24897/6 "2019-06-06T20:26:27Z")

</div>

I change the npdensity(z) of [mcreel](https://discourse.julialang.org/u/mcreel) to work with superior and inferior limits to solve my problem. I want to plot the normal curve to show examples of hypothesis testing.

```julia
using KernelDensity, Plots, Distributions

function npdensity(z,inferior,superior)

    n = size(z,2)
    p = zeros(n)
    for i = 1:n
        x = z[:,i]
        y = kde(x)
        q05 = quantile(x,0.05)
        Plots.plot(range(minimum(x), stop=inferior, length=100),z->pdf(y,z), color=:red, fill=(0,0.5,:red), label="-Zc")
        q95 = quantile(x,0.95)
        Plots.plot!(range(inferior, stop=superior, length=100),z->pdf(y,z), color=:green, fill=(0,0.5,:green),label="Região de Aceitação")
        Plots.plot!(range(superior, stop=maximum(x), length=100),z->pdf(y,z), color = :red, fill=(0,0.5,:red),label="Zc")
        m = mean(x)
        Plots.plot!([m,m],[0,pdf(y,m)],color=:blue, label="média")
        m = median(x)
        if i == 1
            p = Plots.plot!([m,m],[0,pdf(y,m)],color=:black, label="mediana")
        else
            p = [p, Plots.plot!([m,m],[0,pdf(y,m)],color=:black, label="mediana")]
        end
    end
    return p
end

parafusos = rand( Normal( 0 , 1 ),1000000)
npdensity(parafusos,-1.96,1.96)

```
