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

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:

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


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

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) ?

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?

This is the plot of the code:

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?

You can do:

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

1 Like

Alright, thanks a lot @rafael.guerra !

One pattern I use a lot is

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


or even

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

1 Like

Thanks @gustaphe, I like the above structure better.