[Tex/LaTex] How to display labels on points of a tikz plot

tikz-pgf

I'm creating a \begin{tikzpicture} – unclear if I'm working with tike or pgf or whatever honestly, but I have my data displaying perfectly using \addplot[my options] coordinates { .. my data here }. My problem is that for each point of data, I have a label (unique for each data point) that I want to display. How do I do this?

So, in this following example, for these 3 points I want them to show the label that is indicated in the comment there.

\addplot[color=black,solid,thick,mark=*, mark options={fill=white}] coordinates {
         % /32(h)
         (2211, 1110)
         % x1
         (6164, 4168)
         % x1-PPL18
         (11610, 36335)
        };

Best Answer

I would use the nodes near coords feature for this, which allows you to automatically place nodes at each plot coordinate. By default, these nodes will contain the y value, but you can change the value to an arbitrary string provided with each point. This is easiest if you use \addplot table instead of \addplot coordinates, since the input format is less verbose, but you could also do it with coordinates.

If you need to adjust the position of individual labels, you can add a new column to your data and make that available to the node options using visualization depends on=<expression> \as <\macroname>.

Here's an example:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}[xmin=0,ymin=0,enlarge y limits={upper,value=0.2}]
\addplot[
    black,
    thick,
    mark=*,
    mark options={fill=white},
    visualization depends on=\thisrow{alignment} \as \alignment,
    nodes near coords, % Place nodes near each coordinate
    point meta=explicit symbolic, % The meta data used in the nodes is not explicitly provided and not numeric
    every node near coord/.style={anchor=\alignment} % Align each coordinate at the anchor 40 degrees clockwise from the right edge
    ] table [% Provide data as a table
     meta index=2 % the meta data is found in the third column
     ] {
x       y       label       alignment
2211    1110    /32(h)      -40
6164    4168    x1          -40
6500    4500    x2          160
11610   36335   x1-PPL18    -40
};
\end{axis}
\end{tikzpicture}
\end{document}