2

I am trying to foreach over some coordinates to place nodes at whilst inside an axis environment. I followed the advice here, but I am getting nowhere; the following fails with error

! Argument of \pgfmathfloatparse@@ has an extra }.

.

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
   \begin{axis}[
         xmin = -1,
         xmax = 6,
         ymin = -1,
         ymax = 6,
         xlabel = $x$,
         ylabel = $y$,
      ]
      \addplot+[
         nodes near coords,
         only marks,
         point meta=explicit symbolic,
      ]
      coordinates {
         (0,0)        [0]
         (5.65,2.65)  [1]
         (3.12,0.16)  [2]
         (0.83,0.03)  [3]
         (0.20,0.03)  [4]
         (2,5)        [station]
      };

      \coordinate (station) at (axis cs:2,5);

      \foreach \x / \y in {0/0,5.65/2.65,3.12/0.16,0.83/0.03,0.20/0.03,}
      {
         \edef\temp{\noexpand\node (foo) at (axis cs:\x,\y) {hello};}
         \temp
      }

   \end{axis}
\end{tikzpicture}


\end{document}
oarfish
  • 1,419

1 Answers1

2

Delete the trailing comma from here:

      \foreach \x / \y in {0/0,5.65/2.65,3.12/0.16,0.83/0.03,0.20/0.03,}

otherwise, TikZ attempts to place something on an empty coordinate. The complete code:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
   \begin{axis}[
         xmin = -1,
         xmax = 6,
         ymin = -1,
         ymax = 6,
         xlabel = $x$,
         ylabel = $y$,
      ]
      \addplot+[
         nodes near coords,
         only marks,
         point meta=explicit symbolic,
      ]
      coordinates {
         (0,0)        [0]
         (5.65,2.65)  [1]
         (3.12,0.16)  [2]
         (0.83,0.03)  [3]
         (0.20,0.03)  [4]
         (2,5)        [station]
      };

      \coordinate (station) at (axis cs:2,5);

      \foreach \x / \y in {0/0,5.65/2.65,3.12/0.16,0.83/0.03,0.20/0.03}
      {
         \edef\temp{\noexpand\node (foo) at (axis cs:\x,\y) {hello};}
         \temp
      }

   \end{axis}
\end{tikzpicture}


\end{document}

The result:

enter image description here

Gonzalo Medina
  • 505,128