[Tex/LaTex] Label plots in pgfplots without entering coordinates manually

pgfplots

I like to label my plots directly, i.e. without using a legend, but instead using a pin node. So far, I have added the labels with manually specified coordinates, which means trial-and-error for data plots. A better way would be to let TikZ handle the positioning, but I don't know how to access the paths of the plots. Is there a way of adding a node to a plot in pgfplots, similar to the [pos=0.5] option for normal paths? Just adding node [pos=0.5] {x} to the \addplot command does not position the node on the line, but instead at some location in the lower left of the plot area, regardless of the pos value.

MWE:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot {x^2} node [pos=0.75,pin={-10:$x^2$},inner sep=0pt] {}; % This does not position the node on the line
\addplot {1.5*x^2};
\node at (axis cs:4,16) [pin={-10:$x^2$},inner sep=0pt] {}; % This is what I want, but without calculating the coordinates by hand
\node at (axis cs:4,24) [pin={170:$1.5 x^2$},inner sep=0pt] {};
\end{axis}
\end{tikzpicture}
\end{document}

pgfplots plot with nodes annotating the lines

Best Answer

The current version of pgfplots (>= 1.5.1) supports this as you expected it in the first place (i.e. using node[pos=0.75]):

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}
\begin{axis}
\addplot {x^2} node [pos=0.75,pin={-10:$x^2$},inner sep=0pt] {};
\addplot {1.5*x^2} node [pos=0.75,pin={170:$1.5 x^2$},inner sep=0pt] {};
\end{axis}
\end{tikzpicture}


\begin{tikzpicture}
 \begin{axis}[
        axis y line=center,
        axis x line=middle, 
        axis on top=true,
        xmin=-7,
        xmax=7,
        ymin=-4,
        ymax=4,
        clip=false
] 

\addplot[
    mark=none,
    domain=-4:6,
    samples=80,
    red,
    thick,
] {(x<-2)*-2 + (!(x<-2) && (x<3))*x + (!(x<3)) * 3}
    node[pos=0.1,pin=135:{\color{purple}$f(x)=-2$}] {}
    node[pos=0.6,pin=135:{\color{blue}$f(x)=x$}] {}
    node[pos=0.9,pin=135:{\color{green!70!black}$f(x)=3$}] {}
;
\end{axis}
\end{tikzpicture}

\end{document}

It is available for download on http://pgfplots.sourceforge.net/