散点图中的标记和标签颜色相同

散点图中的标记和标签颜色相同

我想获得一个散点图相同颜色的标记和标签。我可以获得彩色标记,但标签仍然保持黑色:

我希望在上面的图中 0 为蓝色,1 为绿色等等。

这是我的 MWE:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{compat=1.12}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xmin=0,
    xmax=1,
    ymin=0,
    ymax=1,
    axis equal image,
    hide axis,
]
\addplot [scatter, only marks, mark=x,
        point meta=explicit,
        nodes near coords*,
        every node near coord/.append style={font=\footnotesize}
    ] table [meta index=2] {points.dat};

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

以及 points.dat 文件

0.399623458645 0.202770745691 1
0.43125083774 0.374183040618 1
0.267449590519 0.086101093226 1
0.498881452071 0.0733728461989 1
0.601248587386 0.607825227731 3
0.663964417031 0.767541727794 3
0.467522971487 0.734379206102 3
0.425463065344 0.554166354467 3
0.106015995305 0.761683705293 2
0.0695936622364 0.917923977313 2
0.0957757803902 0.602873159793 2
0.2631207767 0.484806666192 2
0.919353098154 0.52159970072 0
0.769039639969 0.408860920568 0
0.58319478496 0.256712040273 0
0.6020910719 0.44509904373 0

答案1

根据 overleaf,这是有效的

\documentclass{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=1.12}
\tikzset{adjust near node color/.code={%
    \pgfplotscolormapdefinemappedcolor\pgfplotspointmetatransformed%
    \definecolor{mapped node color}{rgb}{\pgfmathresult}%
    \pgfkeys{/tikz/text=mapped node color!80!black}%
    }
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xmin=0,
    xmax=1,
    ymin=0,
    ymax=1,
    axis equal image,
    hide axis,
]
\addplot [scatter, only marks, mark=x,
        point meta=explicit,
        nodes near coords*,
        every node near coord/.append style={font=\footnotesize,
                                              adjust near node color}
    ] table [meta index=2] {
0.399623458645 0.202770745691 1
0.43125083774 0.374183040618 1
0.267449590519 0.086101093226 1
0.498881452071 0.0733728461989 1
0.601248587386 0.607825227731 3
0.663964417031 0.767541727794 3
0.467522971487 0.734379206102 3
0.425463065344 0.554166354467 3
0.106015995305 0.761683705293 2
0.0695936622364 0.917923977313 2
0.0957757803902 0.602873159793 2
0.2631207767 0.484806666192 2
0.919353098154 0.52159970072 0
0.769039639969 0.408860920568 0
0.58319478496 0.256712040273 0
0.6020910719 0.44509904373 0
};

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

在此处输入图片描述

相关内容