11

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

enter image description here

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]
\pgfplotstableread{
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}
\addplot table[x=xdataa,y=y1dataa] {\sometable};
\addplot table[x=xdataa,y=y2dataa] {\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?

1 Answer 1

13

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.

image

\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

\pgfplotstableread{
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
]
\addplot +[plotlabel={0.75}{blue}] table[x=xdataa,y=y1dataa] {\sometable};

% The following works with the PGFplots version 1.5.1 or higher 
\addplot table[x=xdataa,y=y2dataa] {\sometable} node [pos=0.5, sloped, inner xsep=1pt, inner ysep=0pt, fill=white, anchor=mid] {red};
\end{axis}
\end{tikzpicture}
\end{document}
5
  • 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
    Commented Dec 9, 2011 at 4:49
  • What is \tikz@postactions?
    – Azoun
    Commented Dec 9, 2011 at 8:19
  • 2
    @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). Commented Dec 10, 2011 at 8:13
  • @ChristianFeuersänger: Ah, of course! I tried transform shape, sorry for spreading misinformation!
    – Jake
    Commented Dec 10, 2011 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
    Commented Dec 10, 2011 at 8:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .