1

I'm trying to shade the area between two Bezier curves and two lines, but I can't make it work. Here is an example

\documentclass[12pts]{beamer}
\usepackage{tikz}

\begin{document}

\begin{frame}{Example}
\begin{center}
\begin{tikzpicture}

\coordinate (y) at (0,7);
\coordinate (x) at (7,0);

\draw[<->] (y) node[above] {$P$} -- (0,0) --  (x) node[right]
{$Q$};

\path[line width=0.5mm,red,draw] (0.9,0.1) .. controls (1.5,1) and (1.8,4) .. (2,6) node[above]{$S_i^{1}$};
\path[line width=0.5mm,red,draw] (0.8,0.1) .. controls (2.2,1.5) and (3.2,3.5) .. (4.7,6) node[above]{$S_i^{2}$};
\path[line width=0.5mm,blue,draw] (0.85,0.1) .. controls (2,2) and (3,4) .. (3.7,6) node[above]{$S^{\text{Data}}$};
%  \addplot[gray!50] fill between[of=A and B];
\draw[black,dashed] (0,1.5) node[left]{$P_0$} -- (5.6,1.5);
\draw[black,dashed] (0,5) node[left]{$P_1$} -- (5.6,5);
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}

The output is three Bezier curves, two red and one blue, plus two horizontal lines. I want to shade the areas between each red curve and the blue one, between the lines defined by P_1 and P_2. example_figure.

Thanks.

2

If you were using pgfplots it'd be easy with the fillbetween library, which can also work on tikzpicture with some changes, but you can also just use a clip in this case for the dashed lines and then use the colored lines as contour.

I also added the backgrounds library to place the filling behind the colored lines.

Output

image

Code

\documentclass[12pts]{beamer}
\usepackage{tikz}

\usetikzlibrary{backgrounds}

\begin{document}
\begin{frame}{Example}
\begin{center}
\begin{tikzpicture}
\coordinate (y) at (0,7);
\coordinate (x) at (7,0);

\draw[<->] (y) node[above] {$P$} -- (0,0) --  (x) node[right]
{$Q$};

\draw[line width=0.5mm,red] (0.9,0.1) .. controls (1.5,1) and (1.8,4) .. (2,6) node[above]{$S_i^{1}$};
\draw[line width=0.5mm,red] (0.8,0.1) .. controls (2.2,1.5) and (3.2,3.5) .. (4.7,6) node[above]{$S_i^{2}$};
\draw[line width=0.5mm,blue] (0.85,0.1) .. controls (2,2) and (3,4) .. (3.7,6) node[above]{$S^{\text{Data}}$};

\draw[black,dashed] (0,1.5) node[left]{$P_0$} -- (5.6,1.5);
\draw[black,dashed] (0,5) node[left]{$P_1$} -- (5.6,5);

\begin{scope}[on background layer]
\clip (1,1.5) rectangle (5.6,5);
\fill[green, opacity=.5] (0.9,0.1) .. controls (1.5,1) and (1.8,4) .. (2,6) -- (3.7,6) .. controls (3,4) and (2,2) .. (0.85,0.1) -- cycle;
\fill[red, opacity=.5] (4.7,6) ..controls (3.2,3.5) and (2.2,1.5) .. (0.8,0.1) -- (0.85,0.1) .. controls (2,2) and (3,4) .. (3.7,6) -- cycle;
\end{scope}
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.