# \foreach loop to draw lines

i was trying to use \foreach to read a specific column out of a file. If a value out of this column is lower than e.g. 55 a line should be drawn. I am using a groupplot and the line should be in both graphs and behind both lines. So the graphs should look that way. I haven't found out if it is possible to do that with latex... i just know, if it could work the command has to be after \end{groupplot}.

The way i thought the command could be like that way:

\foreach \xyz <= 55 in {\test} {\draw [red, thick]} (of course it is just an idea...)

The input data is much longer than the minimum example so it is no way to draw it by hand. Another idea was to sort the values out of the file and delete all values greater than 55. With this file I could use \draw I think... but because I have about 125 files it would be much easier to have them just out of the existing files...

I am glad for every idea which is comming up!

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.groupplots}
pos, xyz, abc
1, 56, 1
2, 55, 3
3, 51, 2
4, 64, 1
5, 45, 1
6, 54, 2
7, 25, 1
8, 75, 3
9, 54, 2
10, 64, 1
11, 45, 1
12, 54, 2
}\test

\begin{document}
\begin{tikzpicture}[rotate=-90]
\begin{groupplot}[
group style={
group name=my plots,
group size=1 by 3,
xlabels at=edge bottom,
xticklabels at=edge bottom,
vertical sep=0pt,
},
footnotesize,
width=20cm,
height=8cm,
xlabel=Site,
xmin=0, xmax=13,
ymin=0,
tickpos=left,
]
\nextgroupplot
\nextgroupplot[ymax=100]
\end{groupplot}

%\foreach \xyz <= 55 in {\test} {\draw [red, thick]}

\end{tikzpicture}
\end{document}

• Thanks for the answer. What i am seeing is, that they just use coordinates which they previously handed over. So it would work if i extract the coordinates first. But what i would like to have is that the loop proofs if the value in column 2 is 55 or below and then draw the line at this position (column1). Or did i miss something in the answer? Sep 15 '21 at 16:23

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{pgfplots.groupplots}
\usepackage{pgfplotstable}
pos, xyz, abc
1, 56, 1
2, 55, 3
3, 51, 2
4, 64, 1
5, 45, 1
6, 54, 2
7, 25, 1
8, 75, 3
9, 54, 2
10, 64, 1
11, 45, 1
12, 54, 2
}\test
\begin{document}
\begin{tikzpicture}[rotate=-90]
\begin{groupplot}[
group style={
group name=my plots,
group size=1 by 3,
xlabels at=edge bottom,
xticklabels at=edge bottom,
vertical sep=0pt,
},
footnotesize,
width=20cm,
height=8cm,
xlabel=Site,
xmin=0, xmax=13,
ymin=0,
tickpos=left,
clip=false
]
\nextgroupplot
\nextgroupplot[ymax=100]
\pgfplotstableforeachcolumnelement{xyz}\of\test\as\xyz{%
\ifnum\xyz<55 {
\addplot[red] coordinates {(\pgfplotstablerow+1, 0) (\pgfplotstablerow+1, 200)};
}
\fi
}
\addplot[dashed] coordinates {(0, 55) (13, 55)};
\end{groupplot}
\end{tikzpicture}
\end{document}


• oh dear! thanks a lot! Sep 16 '21 at 12:03
• Is there a possibility to not use "\pgfplotstablerow+1". This means it takes just line after line but i need the value "pos" out of the table. (It is not just 1,2,...,10) Sep 16 '21 at 14:52