Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have loads of pgfplots which have one thing in common, they all containt one or two addplot with four points that I want to label with the same labels, namely: \tau_c= n \theta. Lets for simplicity say n is 1, 2,3 and 4. Using foreach loop variable as node label in pgfplots looks very much like something that could solve my problem. But how do I append this (or similar) solution when the coordinates are given as in the MWE below? My pgfplots are generated with matlab2tikz and are as mentioned quite numerous, so I would like to avoid having to manipulate the existing file too much.

An MWE

\documentclass{memoir}
\usepackage{tikz, pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
% [spy using outlines={rectangle,black,magnification=3,connect spies}]
\begin{axis}[%
width=3in,
height=1in,
unbounded coords=jump,
scale only axis,
xmin=1.25,
xmax=3,
xlabel={That},
ymin=0,
ymax=8,
ylabel={This},
title={Case 11},
% legend style={draw=black,fill=white,legend cell align=left}
]
\addplot [
color=red,
only marks,
mark=o,
mark options={solid},
forget plot
]
table[row sep=crcr]{
2.17534491233797 1.76417509678952\\
1.70352168717505 2.47466020520172\\
1.50385056450878 3.36056987643388\\
1.39330071637349 4.41283273476325\\
};
\end{axis}
\end{tikzpicture}%
\end{document}
share|improve this question
add comment

1 Answer

up vote 9 down vote accepted

You can use the nodes near coords function for that. If you add

\pgfplotsset{
    nodes near coords={
        \pgfmathtruncatemacro\nValue{\coordindex+1}%
        $\nValue \theta$%
    }
}

to your document, the node labels will be printed without you having to touch the matlab2tikz-generated code at all:


If you want to be able to provide arbitrary labels that don't directly depend on the coordinate index, you can use a PGF math array that stores the strings for the labels and access that array within the nodes near coords:

\def\labellist{{"$\sfrac{1}{2}\theta$","$\theta$","$\sfrac{3}{2}\theta$","$2\theta$"}}
\pgfplotsset{
    nodes near coords={
        \pgfmathparse{\labellist[\coordindex]}%
        \pgfmathresult
    }
}

share|improve this answer
    
Amen! Is it possible to somehow make the nodes near coords append to only one or two of many addplots in the same axis environment? Somehow label the addplot, maybe? –  Holene Jun 1 '13 at 22:12
    
@Holene: You can just put the nodes near coords={...} in the \addplot[...] options for the relevant plot. –  Jake Jun 1 '13 at 22:13
    
Thanks! You just saved my night =) –  Holene Jun 1 '13 at 22:14
    
Oh, sorry for the quick return; what if I would have n to be a macro? For instance \sfrac{1}{2}... –  Holene Jun 1 '13 at 22:17
1  
May I upvote twice, please? –  Holene Jun 1 '13 at 22:29
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.