1

I would like to obtain a scatter plot with marks and labels of the same color. I can get colored marks but labels stay stubbornly black:

I would like 0's to be blue, 1's to be green etc. in the plot above.

Here is my 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}

And the points.dat file

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
5

According to overleaf, this works

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

enter image description here

  • Thanks for the great answer. There is still a slight difference in color between the label and the mark, I am trying to understand where it comes from in pgfplots (ugly) code – Xion345 Nov 23 '15 at 18:56
  • Ok, from pgfplotsplothandlers.code.tex (pgfplots_1.12.tds.zip), line 1946: /pgfplots/contour prepared={draw color=mapped color!80!black...}, so to get exactly the same color, it is possible to use \pgfkeys{/tikz/text=mapped node color!80!black} in the code you provided. – Xion345 Nov 23 '15 at 19:06
  • This was really nitpicking, but if I can get a perfect result, why wouldn't I :) Thanks again for the great answer, it helped a lot – Xion345 Nov 23 '15 at 19:11
  • @Xion345 Ah yes. I was thinking too complicated. Nice one. I'm colorblind that's why I didn't notice that. – percusse Nov 23 '15 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.