3

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
]
\addplot coordinates
{
  (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}

enter image description here

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}
\DTLloaddb[noheader=false]{coordinates}{data.csv}

\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.

4

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
]
\addplot+[const plot] table[col sep=comma]{data.csv};
\end{axis}
\end{tikzpicture}
\end{document}

const plot key allow you to have a constant plot.

enter image description here

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
]
\addplot+[jump mark left] table[col sep=comma]{data.csv};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

3

Here is a solution

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

\pgfplotsset{compat=1.12}
\DTLloaddb{coordinates}{data.csv}


\begin{document}

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

\DTLforeach*{coordinates}{\x=x,\y=y}{%
\ifnum\value{DTLrowi}>1
\addplot coordinates {(\mtx,\mty) (\x,\mty)};
\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}
\DTLloaddb{coordinates}{data.csv}


\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
]

\addplot coordinates {\mtplot};
\end{axis}
\end{tikzpicture}


\end{document}

enter image description here

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.