在 PGFplot 轴上绘制具有动态颜色的节点

在 PGFplot 轴上绘制具有动态颜色的节点

我很难理解我的代码在哪里出错了。最后,我想在轴环境中绘制三个点,颜色取决于 x 值。我尝试了多种方法,但下面只给出了两种。你能解释一下为什么这些例子不起作用吗?

\documentclass[tikz,14pt,border=2pt]{standalone}
\usepackage{pgfplots}
\usepackage{xcolor}
\usepackage{filecontents}
\usepackage{csvsimple}

\pgfplotsset{compat=1.11}

\begin{document}
\begin{filecontents}{data.csv}
0.0, 1.0    
3.0, 2.2
4.0, 3.0
\end{filecontents}

\begin{tikzpicture}[auto, thick, >=latex]

\begin{axis}[%
axis x line=center,
axis y line=center,
xmin=-0.5,
xmax=5.5,
ymin=-0.1,
ymax=5.1,
axis background/.style={}
]
    \csvreader[no head]{data.csv}{1=\first, 2=\second}%
    {
        \pgfmathsetmacro{\ccc}{\first*20};
        \colorlet{tempcolor}{red!\ccc!black}

        % does not run
        %\node[draw, circle, color=tempcolor] at (\first,\second){ok};

        % runs but shows only one node
        \pgfmathsetmacro{\xxx}{\first};
        \pgfmathsetmacro{\yyy}{\second};
        \node[draw, circle, color=tempcolor] at (\xxx,\yyy){ok};
    };  
\end{axis}   
\end{tikzpicture}
\end{document}

答案1

这是另一种可行的方法。它依赖于visualization depends on来计算。使用来\ccc放置节点。\addplotmark=text

代码输出

\documentclass[tikz,14pt,border=2pt]{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}

\pgfplotsset{compat=1.11}

\begin{document}
\begin{filecontents}{data.csv}
0.0, 1.0    
3.0, 2.2
4.0, 3.0
\end{filecontents}

\begin{tikzpicture}[auto, thick, >=latex]

\begin{axis}[%
axis x line=center,
axis y line=center,
xmin=-0.5,
xmax=5.5,
ymin=-0.1,
ymax=5.1,
visualization depends on=x*20 \as \ccc
]

\addplot [
   only marks,
   mark=text,
   text mark as node=true,
   text mark style={red!\ccc!black,draw,circle},
   text mark=ok
] table[col sep=comma] {data.csv};

\end{axis}   
\end{tikzpicture}
\end{document}

相关内容