# Iterating through a table using values on the next row

This question is related to Plot Gantt-like plot from csv.

Consider the following file data.cvs

x,y
0,5
4,7
10,5
13,6
17,5
20,7
24,6
28,5


The second column represents some tasks (there are three in this example: 5,6, and 7) and the first column represents time. The table must be interpreted like this:

Each task "activates" from the corresponding value on the first column and extends to the value of the entry in the first column but on the following row. For example, task 5 was active from 0 to 4, from 10 to 13, and from 17 to 20; task 6 was active from 13 to 17, and from 24 to 28; task 7 was active from 4 to 10, and from 20 to 24.

The following code plots each task and shows when the task was "active":

\documentclass[border=3pt]{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=1.12}

\usepackage{filecontents}
\begin{filecontents*}{data.csv}
x,y
0,5
4,7
10,5
13,6
17,5
20,7
24,6
28,5
\end{filecontents*}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
ytick={5,6,7},
xtick=data
]
{
(0,5) (4,5)
(4,7) (10,7)
(10,5) (13,5)
(13,6) (17,6)
(17,5) (20,5)
(20,7) (24,7)
(24,6) (28,6)
};
\end{axis}
\end{tikzpicture}

\end{document}


As you see, I introduced the coordinates manually; the idea is to do it automatically from the file data.csv.

I thought about using the datatool package, but I don't know how to access an element in the next row, not in the current one; schematically I'd use something like:

\documentclass[border=3pt]{standalone}
\usepackage{datatool}
\usepackage{pgfplots}

\pgfplotsset{compat=1.12}

\usepackage{filecontents}
\begin{filecontents*}{data.csv}
x,y
0,5
4,7
10,5
13,6
17,5
20,7
24,6
28,5
\end{filecontents*}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
ytick={5,6,7},
xtick=data
]
%\DTLforeach*{coordinates}{\x=x,\y=y}{
%  \addplot coordinates { (\x,\y) (???,\y) };
%  }
\end{axis}
\end{tikzpicture}

\end{document}


where ???? stands for the element in the first column but in the next row to the current one. Can this be done?

Manually copying the first column makes the task easy, as I did in my answer to the linked question, but I'm looking for an automated way to do it.

Instead of datatools, you can use pgfplots table key, setting the col sep = comma so pgfplots reconize x and y value.

\documentclass[border=3pt]{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=1.12}

\usepackage{filecontents}
\begin{filecontents*}{data.csv}
x,y
0,5
4,7
10,5
13,6
17,5
20,7
24,6
28,5
\end{filecontents*}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
ytick={5,6,7},
xtick=data
]
\end{axis}
\end{tikzpicture}
\end{document}


const plot key allow you to have a constant plot.

EDIT: After seeing the related question, I suggested another key: jump mark left, to get only horizontal line (no vertical ones linking them).

\documentclass[border=3pt]{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=1.12}

\usepackage{filecontents}
\begin{filecontents*}{data.csv}
x,y
0,5
4,7
10,5
13,6
17,5
20,7
24,6
28,5
\end{filecontents*}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
ytick={5,6,7},
xtick=data
]
\end{axis}
\end{tikzpicture}
\end{document}


Here is a solution

\documentclass[border=3pt]{standalone}
\usepackage{datatool}
\usepackage{pgfplots}

\pgfplotsset{compat=1.12}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
ytick={5,6,7},
xtick=data
]

\DTLforeach*{coordinates}{\x=x,\y=y}{%
\ifnum\value{DTLrowi}>1
\fi
\xdef\mtx{\x}\xdef\mty{\y}}
\end{axis}
\end{tikzpicture}

\end{document}


Update: It maybe better to save coordinates and then use it

\documentclass[border=3pt]{standalone}
\usepackage{datatool}
\usepackage{pgfplots}

\pgfplotsset{compat=1.12}

\begin{document}

\def\mtplot{}

\DTLforeach*{coordinates}{\x=x,\y=y}{%
\ifnum\value{DTLrowi}>1
\edef\mtplot{\mtplot(\mtx,\mty) (\x,\mty)}%
\fi
\xdef\mtx{\x}\xdef\mty{\y}}

\begin{tikzpicture}
\begin{axis}[
ytick={5,6,7},
xtick=data
]