How to match color of user-placed labels to contours in pgfplots?

New Answer

This is pure PGFPLOTS answer.

\documentclass[border=5mm]{standalone}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\pgfplotsset{
    label/.style={
        nodes={font=\tiny,fill=white,inner sep=1pt,text=mapped color,#1},
        point meta=explicit,nodes near coords*,nodes near coords align=
    }
}
\begin{tikzpicture}
    \begin{axis}[width=5.0cm,height=5.0cm,xmin=0.0,xmax=1.0,ymin=0.0,ymax=1.0]
        \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
        };
        \addplot[label={rotate=-45}]coordinates{(0.250000E+00, 0.250000E+00)[0.5]};
        \addplot[label={rotate=-45}]coordinates{(0.500000E+00, 0.500000E+00)[1.0]};
        \addplot[label={rotate=-45}]coordinates{(0.750000E+00, 0.750000E+00)[1.5]};
    \end{axis}
\end{tikzpicture}
\end{document}

The key point is that the color is available as mapped color if the color-calculation is turned on. So by point meta=explicit I trigger the color-calculation. Then by nodes near coords* I have a node showing the meta value, which is 0.5, 1.0, 1.5 in your question. Finally nodes near coords align aligns the node properly.

Notice that 1.0 is shown as 1 because the default of nodes near coords* is (line 2859 in pgfplots.code.tex.)

nodes near coords*/.default={\pgfmathprintnumber\pgfplotspointmeta}

Where \pgfplotspointmeta is a FPU-number and \pgfmathprintnumber has its own format. You can alter this by something like

nodes near coords*={\pgfmathprintnumber[fixed zerofill]\pgfplotspointmeta}

Update

To gain the control of explicit output, you need tables.

\documentclass[border=5mm]{standalone}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\pgfplotsset{
    label/.style={
        nodes={font=\tiny,fill=white,inner sep=1pt,text=mapped color!50!black,#1},
        point meta=\thisrow{color},nodes near coords*={\mylabel},nodes near coords align=,
        visualization depends on=value \thisrow{label}\as\mylabel,
    }
}
\begin{tikzpicture}
    \begin{axis}[width=5.0cm,height=5.0cm,xmin=0.0,xmax=1.0,ymin=0.0,ymax=1.0]
        \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
        };
        \addplot[label={rotate=-45}]table{
            x            y            color label
            0.250000E+00 0.250000E+00 0.5   {0.50}
        };
        \addplot[label={rotate=-45}]table{
            x            y            color label
            0.500000E+00 0.500000E+00 1.0   {the one!}
        };
        \addplot[label={rotate=-45}]table{
            x            y            color label
            0.750000E+00 0.750000E+00 1.5   {$\frac32$}
        };
    \end{axis}
\end{tikzpicture}
\end{document}

About Old Answer

PGFPLOTS is hard to understand. So I firstly came up with this pure-TikZ approach. I said "This is a working example" because the approach is rather tedious: A \spy copies the whole axis environment once. Now ask yourself how many labels there are going to be.

Old Answer Below

This is a working example. Perhaps you can automate the code.

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{fadings,spy}
\pgfplotsset{compat=1.12}
\begin{document}
    \tikzset{label/.style={circle,minimum size=10,circle,text=transparent,fill=transparent!0,font=\tiny}}
    \begin{tikzfadingfrompicture}[name=label no 1]
        \path(0,0)node[rotate=-45,label]{0.5};
    \end{tikzfadingfrompicture}
    \begin{tikzfadingfrompicture}[name=label no 2]
        \path(0,0)node[rotate=-45,label]{1.0};
    \end{tikzfadingfrompicture}
    \begin{tikzfadingfrompicture}[name=label no 3]
        \path(0,0)node[rotate=-45,label]{1.5};
    \end{tikzfadingfrompicture}
    \begin{tikzpicture}
        \begin{scope}[spy scope={circle,magnification=20,size=15}]
            \begin{axis}[width=5.0cm,height=5.0cm,xmin=0.0,xmax=1.0,ymin=0.0,ymax=1.0]
                \input{data.tex}
                \coordinate(coord no 1)at(axis cs: 0.750000E+00, 0.750000E+00);
                \coordinate(coord no 2)at(axis cs: 0.500000E+00, 0.500000E+00);
                \coordinate(coord no 3)at(axis cs: 0.250000E+00, 0.250000E+00);
                \spy on(coord no 1)in node at(coord no 1);
                \spy on(coord no 2)in node at(coord no 2);
                \spy on(coord no 3)in node at(coord no 3);
            \end{axis}
        \end{scope}
        \fill[white,path fading=label no 1](coord no 1)circle(25pt);
        \fill[white,path fading=label no 2](coord no 2)circle(25pt);
        \fill[white,path fading=label no 3](coord no 3)circle(25pt);
    \end{tikzpicture}
\end{document}

Decomposition

Use spy library to magnified the line. This step guarantees that the colors match.

\begin{tikzpicture}
    \begin{scope}[spy scope={circle,magnification=20,size=15}]
        \begin{axis}[width=5.0cm,height=5.0cm,xmin=0.0,xmax=1.0,ymin=0.0,ymax=1.0]
            \input{data.tex}
            \coordinate(coord no 1)at(axis cs: 0.750000E+00, 0.750000E+00);
            \coordinate(coord no 2)at(axis cs: 0.500000E+00, 0.500000E+00);
            \coordinate(coord no 3)at(axis cs: 0.250000E+00, 0.250000E+00);
            \spy on(coord no 1)in node at(coord no 1);
            \spy on(coord no 2)in node at(coord no 2);
            \spy on(coord no 3)in node at(coord no 3);
        \end{axis}
    \end{scope}
\end{tikzpicture}

Then prepare the "mask"

\tikz\path(0,0)node[rotate=-45,label,fill=black,text=white]{0.5};
\tikz\path(0,0)node[rotate=-45,label,fill=black,text=white]{1.0};
\tikz\path(0,0)node[rotate=-45,label,fill=black,text=white]{1.5};

The "mask" should be white (black part should be white). And the text is punched out (white part should be transparent). This is implemented by fadings library.

\usetikzlibrary{shadings}
\begin{tikzfadingfrompicture}[name=label no 1]
    \path(0,0)node[rotate=-45,label]{0.5};
\end{tikzfadingfrompicture}
\tikz{
    \shade[shading=color wheel](0,0)rectangle(2,2);
    \fill[white,path fading=label no 1](1,1)circle(75pt);
}

Now combine them all together.

Appendix

Playing with opacity, one can have the label color slightly darker than the line color.

\begin{tikzpicture}
    \begin{scope}[spy scope={circle,magnification=20,size=15}]
        \begin{axis}[width=5.0cm,height=5.0cm,xmin=0.0,xmax=1.0,ymin=0.0,ymax=1.0]
            \input{data.tex}
            \coordinate(coord no 1)at(axis cs: 0.750000E+00, 0.750000E+00);
            \coordinate(coord no 2)at(axis cs: 0.500000E+00, 0.500000E+00);
            \coordinate(coord no 3)at(axis cs: 0.250000E+00, 0.250000E+00);
            \spy on(coord no 1)in node at(coord no 1);
            \spy on(coord no 2)in node at(coord no 2);
            \spy on(coord no 3)in node at(coord no 3);
        \end{axis}
    \end{scope}
    \fill[black,opacity=.5](coord no 1)circle(5pt);
    \fill[black,opacity=.5](coord no 2)circle(5pt);
    \fill[black,opacity=.5](coord no 3)circle(5pt);
    \fill[white,path fading=label no 1](coord no 1)circle(25pt);
    \fill[white,path fading=label no 2](coord no 2)circle(25pt);
    \fill[white,path fading=label no 3](coord no 3)circle(25pt);
\end{tikzpicture}

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.

Tags:

Pgfplots