4

I would like do highlight an area under a plot of data. The first area should be below the plot and above 8 on the y axis. The second area should be below the curve and below the first area and above 6 on the y axis.

Here a sketch and minimal working example (sorry for my poor drawing skills ;) ):

enter image description here

\documentclass{minimal}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{filecontents}
\usetikzlibrary{intersections, pgfplots.fillbetween}

\begin{filecontents}{coordinates.dat}
1 2
2 4
3 5
4 5
5 7
6 9
7 10
\end{filecontents}

\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[name path=data] table {coordinates.dat};

\path [name path=p8] (0,8) -- (7,8);
\path [name intersections={of=p8 and data, by=m}];
\draw [name path=p80, dashed] (m) -- (7,8);
%\tikzfillbetween[of=p80 and data]{blue, opacity=0.1};

\path [name path=p6] (0,6) -- (7,6);
\path [name intersections={of=p6 and data, by=m}];
\draw [name path=p60, dashed] (m) -- (7,6);
%\tikzfillbetween[of=p60 and data]{red, opacity=0.1};

\end{axis}
\end{tikzpicture}
\end{document}

How can I do that?

Maybe tikzfill (see code) is the right way but I don't know how to limit the x and y axis.

2 Answers 2

6

You can make use of the option intersection segments (see chapter 5.7 "Fill between" in the PGFplots manual):

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{fillbetween}

\begin{filecontents}{coordinates.dat}
1 2
2 4
3 5
4 5
5 7
6 9
7 10
\end{filecontents}

\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[name path=data] table {coordinates.dat};

\path [name path=p8] (0,8) -- (7,8);
\path [name intersections={of=p8 and data, by=m}];
\draw [name path=p80, dashed] (m) -- (7,8);

\fill [blue, opacity=0.1, intersection segments={of=data and p8, 
    sequence={L2 -- R2[reverse]}}] -- cycle;

\path [name path=p6] (0,6) -- (7,6);
\path [name intersections={of=p6 and data, by=m}];
\draw [name path=p60, dashed] (m) -- (7,6);

\path [name path=p68, intersection segments={of=data and p6, 
    sequence={L2 -- R2[reverse]}}] -- cycle;

\fill [red, opacity=0.1, intersection segments={of=p68 and p80, 
    sequence={L1 -- L3}}] -- cycle;
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

3
  • Thanks for your solution! I have a hard time to understand the intersection segments sequence. How does that work? Maybe you have a reference in the manual? Mar 6, 2023 at 13:01
  • 1
    You can go to CTAN (ctan.org) where you can find the manual for PGFplots. The fillbetween library is described in chapter 5.7. Mar 6, 2023 at 13:05
  • 1
    The logic of the syntax is like roughly this: you fed this function two paths, the first one is refered to as L (for "left", or as A), the second is refered to as R (for "right", or as B). Both path start somewhere (for closed paths, it is often not very clear where the start is, so one has to try a bit). L1 now means the first part of the first path, which would be upto the point where this path intersects with the other path. R2 would mean the second part of the second path, which would be from the first intersection upto the second intersection with the other path. Mar 6, 2023 at 13:12
3
\begin{filecontents}{coordinates.dat}
1 2
2 4
3 5
4 5
5 7
6 9
7 10
\end{filecontents}

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\begin{scope}
\clip plot file {coordinates.dat} |- cycle;
\filldraw[fill=red!40, dashed] (0,6-|current axis.south west) rectangle (current axis.north east);
\filldraw[fill=blue!40, dashed] (0,8-|current axis.south west) rectangle (current axis.north east);
\end{scope}
\addplot+[thick] table {coordinates.dat};
\end{axis}
\end{tikzpicture}
\end{document}

Graph with segment curve and two filled areas

3
  • Thanks for this wonderful solution. However, I plot the data from a CSV file and\clip plot table [x=index, y=col2, col sep=comma] {data.csv} |- cycle; obviously does not work. How would I do that? Mar 6, 2023 at 12:58
  • Can I reuse the path from \addplot somehow? Mar 6, 2023 at 13:00
  • 1
    Yes, you can give the option save path=\pathA to the plot, and then use \clip[use path=\pathA];. Still no extra packages are needed Mar 6, 2023 at 22:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .