# Why does datatool/pgfplots only plot for last row in table?

I'm trying to use datatool to put a bunch of circles on a pgfplots plot. I tried the following:

\documentclass{article}
\usepackage{datatool,tikz,pgfplots}
\pgfplotsset{compat=1.9}

\begin{filecontents}{coord.dat}
x,y
0,0
5,5
-3,4
\end{filecontents}

\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=-10,xmax=10,ymin=-10,ymax=10,axis equal]
\DTLforeach*{coordinates}{\x=x, \y=y}{\draw (axis cs:\x,\y) circle[radius=2] ;}
\end{axis}
\end{tikzpicture}
\end{document}


But only one circle is plotted, at the coordinates from the last row in the table (i.e., centered at (-3,4)).

I get 3 circles if I put the \DTLforeach* into a bare tikzpicture environment, so it looks like some problem between pgfplots and datatool. Or am I doing something wrong?

• Why are you using datatool here rather than just plotting the numbers directly using \addplot? – Joseph Wright Jan 30 '14 at 7:08
• pgfplots doesn't draw curves immediately. first it collects all the plotting commands and determines the axis options. so your variables stay \x etc until the final moment. And when it expands it only sees the final values. hence 3 identical circlea are overdrawn. indeed why don't you use \addplot ? – percusse Jan 30 '14 at 7:41
• There's an example for plotting circles from a data file in PGFPlots at tex.stackexchange.com/a/20658/2552 – Jake Jan 30 '14 at 7:48
• Well, what I'm really trying to do is plot a series of ellipses all with different major/minor axis and rotation angles. I don't think I can do that with \addplot, can I? I had already found tex.stackexchange.com/questions/20496/…, and the datatool solution looked by far the simplest. – Dan Becker Jan 30 '14 at 15:55

As Percusse pointed out, PGFPlots first collects all TikZ commands (like \draw) and then executes them at the very end of the axis environment, which leads to the last circle being drawn three times: during every loop iteration, another \draw (axis cs:\x,\y)... is added to the list of commands, and at the very end, when \x and \y contain -3 and -4, respectively, these \draw commands are executed.

You can tell PGFPlots to also defer the \DTLforeach loop to the end of the axis environment by wrapping it in \pgfplotsextra:

\documentclass{article}
\usepackage{datatool,tikz,pgfplots}
\pgfplotsset{compat=1.9}

\begin{filecontents}{coord.dat}
x,y
0,0
5,5
-3,4
\end{filecontents}

`