# How to Fill Specific Range of a Function with Layouts?

**URL:** <https://discourse.julialang.org/t/how-to-fill-specific-range-of-a-function-with-layouts/91732>\
**Category:** General Usage\
**Tags:** question, plotting, plots, gr\
**Created:** [December 16, 2022, 12:27pm UTC](https://discourse.julialang.org/t/how-to-fill-specific-range-of-a-function-with-layouts/91732 "2022-12-16T12:27:56Z")\
**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:** [December 16, 2022, 12:27pm UTC](https://discourse.julialang.org/t/how-to-fill-specific-range-of-a-function-with-layouts/91732/1 "2022-12-16T12:27:56Z")

</div>

Hi all,

I want to fill the area under the function from 0 to 2 \pi, it can be done easily if I only have layout of 1 plot just use `plot!`.

The guide only show this and not very useful:

```julia
    fillrange = 1,
    fillalpha = 0.5,
    fillcolor = :red,

```

I want to show 2 plots, thus how to fill the area with this layout of 2 ?

```julia
using Plots, LaTeXStrings, Plots.PlotMeasures, SymPy
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

f(x) = (sin(x))^2
g(x) = (cos(x))^2
a, b = -2π, 2π

p1 = plot(f, a, b; ylims= (0,2), xtick=pitick(a, b, 1; mode=:latex), framestyle=:zerolines,
	legend=:topright, label=L" f(x) = \sin^{2} x ", fill=(0, 0.15, :green),
	size=(720, 360), tickfontsize=10)
p2 = plot(g, a, b; ylims= (0,2), xtick=pitick(a, b, 1; mode=:latex), framestyle=:zerolines,
	legend=:topright, label=L" f(x) = \cos^{2} x ", fill=(0, 0.15, :green),
	size=(720, 360), tickfontsize=10)

plot(p1, p2, layout = (2, 1), legend=:outerright,
     xaxis = "x", yaxis = "y(x)")
#plot!(f,0,2π, label="", fill=(0, 0.15, :green))

```

How should I tweak or tinker this `fill=(0, 0.15, :green)` ?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 16, 2022, 1:49pm UTC](https://discourse.julialang.org/t/how-to-fill-specific-range-of-a-function-with-layouts/91732/2 "2022-12-16T13:49:41Z")

</div>

I’m not sure I understand, but you could try using the plot keyword `subplot=1` or `subplot=2`, to redirect your actions to each subplot.

NB:  
`SymPy` doesn’t seem to be used in your MWE?

---

<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:** [December 16, 2022, 2:30pm UTC](https://discourse.julialang.org/t/how-to-fill-specific-range-of-a-function-with-layouts/91732/3 "2022-12-16T14:30:52Z")

</div>

This is the plot of the code:

 ![Capture d’écran_2022-12-16_21-26-38](https://global.discourse-cdn.com/julialang/original/3X/b/8/b8d21b6eb877eb481c4c05e608cbefa4d4630c39.png)

I want the fill of both plots to be from 0 to 2 \pi not from - 2 \pi to 2 \pi

How to add `subplot=1` in the code?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 16, 2022, 3:26pm UTC](https://discourse.julialang.org/t/how-to-fill-specific-range-of-a-function-with-layouts/91732/4 "2022-12-16T15:26:18Z")

</div>

You can do:

```julia
plot!(f,0,2π, label="", fill=(0, 0.15, :green), subplot=1)
plot!(g,0,2π, label="", fill=(0, 0.15, :green), subplot=2)

```

---

<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:** [December 17, 2022, 7:32am UTC](https://discourse.julialang.org/t/how-to-fill-specific-range-of-a-function-with-layouts/91732/5 "2022-12-17T07:32:14Z")

</div>

Alright, thanks a lot @rafael.guerra !

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [December 17, 2022, 8:25am UTC](https://discourse.julialang.org/t/how-to-fill-specific-range-of-a-function-with-layouts/91732/6 "2022-12-17T08:25:23Z")

</div>

One pattern I use a lot is

```julia
pl1 = plot(...)
plot!(pl1, ...)
pl2 = plot(...)
plot(pl1, pl2; layout=(1, 2))

```

or even

```julia
plot(
    begin
        plot(...)
        plot!(...)
    end,
    plot(...);
    layout=(1, 2)
)

```

---

<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:** [December 17, 2022, 9:52am UTC](https://discourse.julialang.org/t/how-to-fill-specific-range-of-a-function-with-layouts/91732/7 "2022-12-17T09:52:10Z")

</div>

Thanks @gustaphe, I like the above structure better.
