# pgfplot fillbetween

I need a plot beween f, t and the x-axis, but do not know how and could not find a solution here. Here is a wrong solution

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns} % Hinzufügen der patterns-Bibliothek
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
axis x line = center,
ylabel={$y$},
axis y line = center,
domain=.1:11,
xmin=-1, xmax=4,
ymin=-1, ymax=4, % Anpassung der y-Skalierung
samples=100,
smooth,
no markers,
legend pos=north east,
grid=major, % Gitter im Hintergrund einschalten
]
\addlegendentry{$f(x)=\frac{2}{x^2}$}
\addlegendentry{$t(x)$}
\addplot[gray!50, pattern=north east lines] fill between[of=f and axis, soft clip={domain=1:5}]; % Fläche unter f
\addplot[gray!50, pattern=north west lines] fill between[of=t and axis, soft clip={domain=1:5}]; % Fläche unter t
\path[name path=xaxis] (\pgfkeysvalueof{/pgfplots/xmin}, 0) -- (\pgfkeysvalueof{/pgfplots/xmax},0);
\end{axis}
\end{tikzpicture}
\end{document}
• You need to fill area between f ad t on the domain 1:1.5 plus the area between f and x axis on the domain 1.5:4. Nov 16, 2023 at 18:30
• Thanks! Will do so, this works. But is there a possibility to define the filling between three curves? Nov 16, 2023 at 18:44
• Yes! You proceed in the same way. I cant done more details. I need to see the concrete situation. Nov 16, 2023 at 18:50
• You could look into the pgfplots manual and search for intersection segments. As an example: tex.stackexchange.com/a/678445/47927 . Nov 16, 2023 at 19:01

Using the intersections segments option (see the relevant section in the pgfplots manual), you could do the following:

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns} % Hinzufügen der patterns-Bibliothek
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
axis x line=center,
ylabel={$y$},
axis y line=center,
domain=.1:11,
xmin=-1, xmax=4,
ymin=-1, ymax=4, % Anpassung der y-Skalierung
samples=100,
smooth,
no markers,
legend pos=north east,
grid=major, % Gitter im Hintergrund einschalten
]
\addlegendentry{$f(x)=\frac{2}{x^2}$}
\addlegendentry{$t(x)$}
\addplot[name path=axis, draw=none, domain=-1:4] {0}; % x-Achse

\path[name path=f-t, intersection segments={
of=f and t,
sequence={
L7[reverse] -- R7
},
}] -- cycle;

\fill[pattern=north east lines, intersection segments={
of=f-t and axis,
sequence={
R2 -- L1
},
}] -- cycle;
\end{axis}
\end{tikzpicture}
\end{document}

In the first step, I created a path that descibes an area limited by the two plots f and t which I name f-t. Then, I created the path that describes the area limited by f-t and axis.

Note that here, probably due to rounding errors, the curve of path f seems be interpreted as intersecting the straight line t several times which is why we need to choose the seventh section of the sequence. Typially, you won't have such an effect and it might change if you alter the number of samples.