# Region Bounded Between $\\sin (x)$ and $\\cos (x)$

**URL:** <https://discourse.julialang.org/t/region-bounded-between-sin-x-and-cos-x/101008>\
**Category:** Visualization\
**Tags:** question, plotting, plots\
**Created:** [June 30, 2023, 4:51am UTC](https://discourse.julialang.org/t/region-bounded-between-sin-x-and-cos-x/101008 "2023-06-30T04:51:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [June 30, 2023, 4:51am UTC](https://discourse.julialang.org/t/region-bounded-between-sin-x-and-cos-x/101008/1 "2023-06-30T04:51:32Z")

</div>

Hi all,

I want to ask whether I am right that the triangle region between 0 and \pi/2 should not be included for region bounded by \sin (x) and \cos (x) between x = - \pi / 4 and x = 3 \pi / 4 ?

But, In the plot below it becomes dark blue since it is filled twice. How to make it all correctly filled so none is darker than the other?

 ![Capture d’écran_2023-06-30_11-36-25](https://global.discourse-cdn.com/julialang/original/3X/d/4/d4e77674289f1ab2942d188c665a258b184efd47.png)

```julia
using Plots, LaTeXStrings, Plots.PlotMeasures, CalculusWithJulia
gr()

function pitick(start, stop, denom; mode=:text)
    a = Int(cld(start, π/denom))
    b = Int(fld(stop, π/denom))
    tick = range(a*π/denom, b*π/denom; step=π/denom)
    ticklabel = piticklabel.((a:b) .// denom, Val(mode))
    tick, ticklabel
end

function piticklabel(x::Rational, ::Val{:text})
    iszero(x) && return "0"
    S = x < 0 ? "-" : ""
    n, d = abs(numerator(x)), denominator(x)
    N = n == 1 ? "" : repr(n)
    d == 1 && return S * N * "π"
    S * N * "π/" * repr(d)
end

function piticklabel(x::Rational, ::Val{:latex})
    iszero(x) && return L"0"
    S = x < 0 ? "-" : ""
    n, d = abs(numerator(x)), denominator(x)
    N = n == 1 ? "" : repr(n)
    d == 1 && return L"%$S%$N\pi"
    L"%$S\frac{%$N\pi}{%$d}"
end

a, b = -2π, 2π

y(x) = sin(x)
y2(x) = cos(x)
xs1 = range(a, b, length=150)

plot(xs1, y, color=:red, xtick=pitick(a, b, 4; mode=:latex), 
	xlims=(-3,3), ylims=(-π/2,π/2),framestyle=:zerolines,
	linestyle=:solid, linecolor=:red2,
	legend=:topleft, label=L"y = \sin \ x", 
	bottom_margin=3mm,
	size=(800, 460), tickfontsize=10)

plot!(xs1,y2, label=L"y = \cos \ x")

plot!(y,-π/4,3π/4, label="", fill=(0, 0.15, :blue))
plot!(y2,-π/4,3π/4, label="", fill=(0, 0.15, :blue))

```

**I tried `PolygonArea` and use `plot!(y2 \ y, color=:green) ` → not working**

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [June 30, 2023, 5:25pm UTC](https://discourse.julialang.org/t/region-bounded-between-sin-x-and-cos-x/101008/2 "2023-06-30T17:25:52Z")

</div>

try implicit functions.  
I don’t know much. I got there by trial and error.

 ![btw_sin_cos](https://global.discourse-cdn.com/julialang/original/3X/8/2/824afa687602946ab3a5c12abb877ff17f2a9a7c.jpeg)

```julia
julia> using Plots, LaTeXStrings, Plots.PlotMeasures, CalculusWithJulia

julia> gr()
Plots.GRBackend()

julia> using ImplicitEquations

julia> f(x,y) = y-sin(x)
f (generic function with 1 method)

julia> g(x,y) = y-cos(x)
g (generic function with 1 method)

julia> function pitick(start, stop, denom; mode=:text)
           a = Int(cld(start, π/denom))
           b = Int(fld(stop, π/denom))
           tick = range(a*π/denom, b*π/denom; step=π/denom)
           ticklabel = piticklabel.((a:b) .// denom, Val(mode))
           tick, ticklabel
       end
pitick (generic function with 1 method)

julia> a, b = -2π, 2π
(-6.283185307179586, 6.283185307179586)

julia> plot((f ≫ 0) & (g ≦ 0) , fc=:gray,xtick=pitick(a, b, 4; mode=:latex), xlims=[-π/4,π/4],lims=[-1,1], aspect_ratio=:equal)

julia> plot!((g ≫ 0) & (f ≦ 0) , fc=:gray,xtick=pitick(a, b, 4; mode=:latex), xlims=[-π/4,3π/4],lims=[-1,1], aspect_ratio=:equal)

```

PS  
you could try with two colors that “added” give white

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [June 30, 2023, 7:12pm UTC](https://discourse.julialang.org/t/region-bounded-between-sin-x-and-cos-x/101008/3 "2023-06-30T19:12:53Z")

</div>

Try the following:

```julia
plot(xs1, y, color=:red, xtick=pitick(a, b, 4; mode=:latex), 
     xlims=(-3,3), ylims=(-π/2,π/2),framestyle=:zerolines,
     linestyle=:solid, linecolor=:red2,
     legend=:topleft, label=L"y = \sin \ x", 
     bottom_margin=3mm,
     size=(800, 460), tickfontsize=10)

plot!(xs1,y2, label=L"y = \cos \ x")

plot!(y2,-π/4,3π/4, label="", fillrange=y,fillalpha=0.3, fillcolor=:blue)

```

The trick was to use `fillrange` attribute. It’s in the documentation, but perhaps a little obscure.

P.S. question is phrased in reader friendly way, with a working example. Thanks.

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 2, 2023, 10:50am UTC](https://discourse.julialang.org/t/region-bounded-between-sin-x-and-cos-x/101008/4 "2023-07-02T10:50:35Z")

</div>

Thanks for this. Really appreciate it.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [July 2, 2023, 11:41am UTC](https://discourse.julialang.org/t/region-bounded-between-sin-x-and-cos-x/101008/5 "2023-07-02T11:41:02Z")

</div>

I would prefer Dan’s soluzioni.

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 2, 2023, 11:44am UTC](https://discourse.julialang.org/t/region-bounded-between-sin-x-and-cos-x/101008/6 "2023-07-02T11:44:39Z")

</div>

This is a nice solution too. Thanks a lot.

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 2, 2023, 11:45am UTC](https://discourse.julialang.org/t/region-bounded-between-sin-x-and-cos-x/101008/7 "2023-07-02T11:45:09Z")

</div>

Yes, but you give another way of thinking by using Implicit Equation instead of just `Plots`. So I learn more here.
