# Contour plot labeling applied to ordinary 2D plots

I was trying to obtain the effect that I manually created below,shown with the letter n. Now, I know that this is customary to do in contour plots, hence I've checked the manual and saw that it is indeed possible with pgfplots

I thought: OK, why don't you just fake a contour plot?

\documentclass{article}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{tikzpicture}[scale=0.7]
\begin{axis}[%
grid=both,no markers,scaled ticks = false,
tick label style={/pgf/number format/.cd,fixed,precision=3},
ylabel=Sheep, xlabel = Count]
xdataa    y1dataa   y2dataa
0.0100    0.0909    0.0336
0.0120    0.0841    0.0307
0.0140    0.0777    0.0288
0.0160    0.0728    0.0258
0.0180    0.0701    0.0241
0.0200    0.0662    0.0223
0.0220    0.0627    0.0210
0.0240    0.0594    0.0200
0.0260    0.0570    0.0190
0.0280    0.0534    0.0176
0.0300    0.0509    0.0169
0.0320    0.0488    0.0160
0.0340    0.0466    0.0151
0.0360    0.0446    0.0142
0.0380    0.0429    0.0138
0.0400    0.0410    0.0132
}{\sometable}
\node[fill=white,rotate=-25,inner sep=1mm] at (15mm,34.5mm) {n};
\end{axis}
\end{tikzpicture}
\end{document}


I couldn't do it (the plots disappear for some reason whenever I define the contour prepared option) or I am simply blinded to see some magic option but had I been successful this would have the following immediate drawbacks:

1. I can't include any non-numerical labels such as n in the example.
2. If I had to use the same label for different curves they would automatically merged due to the common contour value.

I also came up with a solution using the unbounded coords=jump option to break the curve. But this is simply ugly and too pragmatic (even for me).

Obviously, if contour plot can do it, there must be a way to hack into the mechanism to obtain the relevant code and carry it over to the regular 2D plot case. I tried to tweak the code but no avail. Is there a better and systematic way which reduces the manual labor especially adding another column?

-

Since version 1.5.1, PGFplots can place nodes along a graph using node [pos=<pos>] {<text>}. If you supply the sloped option, the node will be rotated according to the slope of the graph.

If for some reason you're stuck with an older version, you could use a decoration for this. Here's a style called plotlabel={<pos>}{<text>} that's adapted from my answer to Label plots in pgfplots without entering coordinates manually. It uses the approach described in Applying a postaction to every path in TikZ to apply a postaction to the plot using every path/.style without getting stuck in a loop.

\documentclass{article}
\usepackage{tikz,pgfplots}
\usetikzlibrary{decorations.markings}
\begin{document}
\makeatletter
\tikzset{
nomorepostaction/.code={\let\tikz@postactions\pgfutil@empty},
plotlabel/.style 2 args={
every path/.append style={
postaction={
nomorepostaction,
decorate,
decoration={
markings,
mark=at position #1 with {\pgfset{inner ysep=0pt, inner xsep=1pt}
\pgfnode{rectangle}{east}{\color{black}#2}{}{\color{white}\pgfusepath{fill}}
}
}
}
}
}
}
\makeatother

xdataa    y1dataa   y2dataa
0.0100    0.0909    0.0336
0.0120    0.0841    0.0307
0.0140    0.0777    0.0288
0.0160    0.0728    0.0258
0.0180    0.0701    0.0241
0.0200    0.0662    0.0223
0.0220    0.0627    0.0210
0.0240    0.0594    0.0200
0.0260    0.0570    0.0190
0.0280    0.0534    0.0176
0.0300    0.0509    0.0169
0.0320    0.0488    0.0160
0.0340    0.0466    0.0151
0.0360    0.0446    0.0142
0.0380    0.0429    0.0138
0.0400    0.0410    0.0132
}{\sometable}

\begin{tikzpicture}
\begin{axis}[%
grid=both,
no markers
]

Ah, of course! I used the text along path decoration but the letters were too ... funky :) I didn't think about the markings. As always, thanks for the neat answer! +1 whenever I get rid of the vote cap. – percusse Dec 9 '11 at 4:49
@Jake the node at fraction feature of the unstable version supports the sloped key which will automatically rotate the node (just as the sloped key in tikz). – Christian Feuersänger Dec 10 '11 at 8:13
@ChristianFeuersänger: Ah, of course! I tried transform shape, sorry for spreading misinformation! – Jake Dec 10 '11 at 8:33
@Azoun: That macro contains the actions that are applied to TikZ paths. They need to be cleared if supplied to every path, otherwise you'll get an infinite recursion. See tex.stackexchange.com/a/5354/2552 – Jake Dec 10 '11 at 8:48