如何使用 \foreach 将节点放置在 pgfplots 轴中?

如何使用 \foreach 将节点放置在 pgfplots 轴中?

我正在尝试foreach在某个axis。我遵循了建议这里,但我无所获;以下操作失败并出现错误

! 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}

答案1

从此处删除尾随的逗号:

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

否则,TikZ 会尝试将某些东西放置在空坐标上。完整代码:

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

结果:

在此处输入图片描述

相关内容