[Tex/LaTex] How to match color of user-placed labels to contours in pgfplots

pgfplots

This question is a follow-up to this one about manually placing contour labels in pgfplots contour plots produced with the contour prepared option. Briefly put, the problem I'm faced with now is that the manually placed contour labels are all in black (or any other color I want) and do not match the color of the contour lines. For example, see:

enter image description here

So my question is: How do I modify the files below so that the label colors match the color of the contour lines as they would if the labels had been generated by pgfplots?

What I'm looking for in the solution:

  1. For obvious reasons, I would like the color matching to be automatic and not have to be done by hand. Based on the pgfplots manual, this would appear to require something using mapped color!50!black but I have not been able to get this to work.
  2. It would be great if the solution would also work if I decided to skip labeling every contour line (for example, if they are equispaced and close to each other). (To be specific, if I were to label only the 0.5 and 1.5 levels in the plot above, the colors should remain blue and red – the 1.5 level should not be orange because the 1.0 level was skipped.)

Here is the MWE:

\documentclass[border=5mm]{standalone}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
  \begin{tikzpicture}[scale=1.0,
    label/.style={align=center,fill=white,inner sep=1pt,font=\tiny}]
    \begin{axis}[width=5.0cm,height=5.0cm,xmin=0.0,xmax=1.0,ymin=0.0,ymax=1.0]
      \input{contour-data.tex}
      \input{contour-labels.tex}
    \end{axis}
  \end{tikzpicture}
\end{document}  

Here is data.tex:

\addplot[contour prepared={labels=false}]
table {
  0.500000E+00  0.000000E+00  0.500000E+00
  0.000000E+00  0.500000E+00  0.500000E+00

  0.100000E+01  0.000000E+00  0.100000E+01
  0.500000E+00  0.500000E+00  0.100000E+01
  0.000000E+00  0.100000E+01  0.100000E+01

  0.500000E+00  0.100000E+01  0.150000E+01
  0.100000E+01  0.500000E+00  0.150000E+01 
};

And here is labels.tex:

\node[label,rotate=-45.0] at (axis cs: 0.250000E+00, 0.250000E+00) {0.5};
\node[label,rotate=-45.0] at (axis cs: 0.500000E+00, 0.500000E+00) {1.0};
\node[label,rotate=-45.0] at (axis cs: 0.750000E+00, 0.750000E+00) {1.5};

Best Answer

You seem to wonder, where the colors are chosen from. They come from the actual colormap, which by default is colormap name=hot. This can be visualized easily when activating the colorbar. You will find a lot of colormaps in the manual or even can create one yourself, if you want.

With the new release of PGFPlots v1.13 it is easy to pick a color from the colormap with the key color of colormap which can be found in section 4.7.6 on page 193 of the manual. The drawback is, that the number is expected to be in the interval [0,1000] so you "manually" have to compute it from the z or meta values. Here is the code using this new feature.

\documentclass[border=2mm]{standalone}
\usepackage{tikz,pgfplots}
    \pgfplotsset{compat=1.11}
\begin{document}
    \begin{tikzpicture}[
        label/.style={
            align=center,fill=white,inner sep=1pt,font=\tiny,
            %% adds additional space in front of the text which has exactly the
            %% size of "!50!white" and is considered being a bug.
            %% (To verify that this length is added, uncomment the last node
            %%  at the end of the axis and you will notice, that the "1.5"s
            %%  are overlapping each other.)
            %text=.!40!white,     % <-- `.' equals the actual color
            %
            % to avoid this bug, just turn the color definition around
            text=white!60!.,     % <-- `.' equals the actual color
        },
    ]
        \begin{axis}[
            width=5.0cm,height=5.0cm,xmin=0.0,xmax=1.0,ymin=0.0,ymax=1.0,
            colorbar,           % just to show where the colors come from
            colormap/viridis,   % <-- change colormap
            % change min and max values of the colorbar
            point meta min=0,point meta max=2,
        ]
            \addplot[contour prepared={labels=false,}] table {
                0.500000E+00  0.000000E+00  0.500000E+00
                0.000000E+00  0.500000E+00  0.500000E+00

                0.100000E+01  0.000000E+00  0.100000E+01
                0.500000E+00  0.500000E+00  0.100000E+01
                0.000000E+00  0.100000E+01  0.100000E+01

                0.500000E+00  0.100000E+01  0.150000E+01
                0.100000E+01  0.500000E+00  0.150000E+01
            };

            \node[color of colormap=250,label,rotate=-45.0]
                at (0.250000E+00, 0.250000E+00) {0.5};
            \node[color of colormap=500,label,rotate=-45.0]
                at (0.500000E+00, 0.500000E+00) {1.0};
            \node[color of colormap=750,label,rotate=-45.0]
                at (0.750000E+00, 0.750000E+00) {1.5};

            %% to show the bug, uncomment the folowing lines and switch
            %% to the other `text' key in the definition of the `label' style
            %\node[align=center,inner sep=1pt,font=\tiny,rotate=-45.0]
            %    at (0.750000E+00, 0.750000E+00) {!40!white1.5};
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

To avoid doing this manual calculation the "New Answer" of Symbol 1 is probaply the best you can get.