# Loop over pgfplotstable rows

I wan't to draw tikz picture based on data loaded from pgf table. Can I somehow use it like standard \foreach?

\foreach \i/\j in {\mytable}
\draw...


What I found is \pgfplotstableforeachcolumn but it iterates in wrong dimension and makes column as one.

I was thinking about typesetting the table \pgfplotstabletypeset but it looks like there is no way to get rid of tabular environment out of it.

Are there any ready made ways to iterate over the pgfplotstable? It there way to typeset it in format required by tikz foreach?

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}
1 2
2 3

% Should behave like this:
\begin{tikzpicture}
\foreach \i/\j \in {1/2,2/3}
\draw (0,0) -- (\i,\j);
\end{tikpicture}

% and look like this
\begin{tikzpicture}
\draw (0,0) -- (\i,\j);
\end{tikpicture}

% or this
\begin{tikzpicture}
\draw (0,0) -- (\i,\j);
\end{tikpicture}
\end{document}

• Please provide a minimal working example (MWE) with an example table and document so we see what you are trying to acieve.
– Sam
Nov 11, 2020 at 10:13
• @Sam MWE is the solution... Nov 11, 2020 at 10:16
• You're right, but let's just call it a minimal not working example then. You see, it's hard to imagine what your table looks like if we don't see it. Please provide more information. Your question leaves all the effort to our community, even typing the essentials of a TeX document such as \documentclass{}...\begin{document} etc. As it is, most of our users will be very reluctant to touch your question, and you are left to the mercy of our procrastination team who are very few in number and very picky about selecting questions.
– Sam
Nov 11, 2020 at 10:19
• Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers.
– Sam
Nov 16, 2020 at 11:48

You can pull off individual elements using \pgfplotstablegetelem.

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}
x y
1 2
2 3

% and look like this
\begin{tikzpicture}
\foreach \row in {0,1} {
\let\x=\pgfplotsretval
\let\y=\pgfplotsretval
\draw (0,0) -- (\x,\y);
}
\end{tikzpicture}
\end{document}


After some fiddling around I came up with a solution that still leaves plenty of room for improvement. For example, I don't know how to instruct pgfplots to use the cycle list to color the lines, furthermore, the axis boundaries have to be set manually at the moment. Here an automated determination would be more practical.

Nevertheless, I don't want to withhold my idea from you, maybe this is already enough for your needs.

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{csvsimple}

\pgfplotsset{compat = 1.17}

\begin{document}

\begin{filecontents*}{data.csv}
x,y
1,2
2,3
3,4
4,5
\end{filecontents*}

\begin{tikzpicture}
\begin{axis}[%
xmin = 0,
xmax = 4,
ymin = 0,
ymax = 5,
enlargelimits%
]
\pgfplotsextra{
{x = \x, y = \y}%
{%
\pgfpathmoveto{\pgfplotspointaxisxy{0}{0}}
\pgfpathlineto{\pgfplotspointaxisxy{\x}{\y}}
\pgfusepath{stroke}
}
}
\end{axis}
\end{tikzpicture}

\end{document}


## Edit

I have now spent some more time with this exciting problem and have finally been able to fix all the issues mentioned above.

The csvsimple package makes it easy to read .csv files and with the \csvloop command, it is easily possible to execute a command for each line of the file. So I created a command that appends its content to a list each time it is called. This command is used in \csvloop and generates a list with all coordinates.

Within the axis-environment, a \foreach command can now be used to read the coordinates from the list and add a plot from (0,0) to (x,y) for each coordinate.

\documentclass{article}

% Packages
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{csvsimple}
\usepackage{etoolbox}

% Package settings
\pgfplotsset{compat = 1.17}

% Define custom macros to parse the table content into a list
\newcommand{\coordinates}{}
\newcommand{\coordinate}[1]{%
\ifdefempty{\coordinates}
{\xappto\coordinates{#1}}
{\xappto\coordinates{,#1}}
}

\begin{filecontents*}{mydata.csv}
x,y
1,2
2,3
3,4
4,5
\end{filecontents*}

\begin{document}

% Read the table and parse it to a list
\csvloop{%
file = {mydata.csv},%
column names = {x=\x, y=\y},%
command = \coordinate{\x/\y}%
}

\begin{tikzpicture}
\begin{axis}[%
enlargelimits%
]
\foreach \x/\y in \coordinates {