26

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)
        };
qubyte
  • 17,299
Nektarios
  • 403

2 Answers2

28

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}
Jake
  • 232,450
  • Some of my figures include points very near each other; I think this would have my labels stomp on each other because of the anchor=-40 degrees - is there a way to specify this with no 'trampling'? I don't see a way – Nektarios Mar 12 '12 at 03:46
  • You can provide a per-point alignment by adding a new column and using visualization depends on. I've edited my answer. – Jake Mar 12 '12 at 03:57
  • What about cases where I may need to have the labels be at different distances from the points? I've actually made up this graph now using Peter's method and I had to significantly shift the x,y values I gave each label. I prefer your way but is there another column of that table that can add distance? – Nektarios Mar 12 '12 at 04:18
  • 1
    You can add as many columns as you want, and make each available in the same way as the first. You could add a padding column and map that to the outer sep in the node style, or you could add an xshift and a yshift column and map it to the options of the same name. – Jake Mar 12 '12 at 04:24
  • 1
    @grfrazee: You can add fill=white, inner sep=1pt to the every node near coord/.style={...}. – Jake Mar 27 '17 at 18:34
  • It seems that the alignment column can only contain numeric data. Is it possible to use the 'east' syntax somehow? – user172294 Jun 26 '21 at 11:46
14

Jake's solution is a better, but as an alternative you could add nodes at each of the coordinates:

enter image description here

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[color=black,solid,thick,mark=*, mark options={fill=white}] 
    coordinates {
         % /32(h)
         (2211, 1110)
         % x1
         (6164, 4168)
         % x1-PPL18
         (11610, 36335)
        }; 
\node [above] at (axis cs:  2211,  1110) {$32h$};
\node [below] at (axis cs:  6164,  4168) {$x_1$};
\node [left ] at (axis cs: 11610, 36335) {$x_1-PPL_{18}$};
\end{axis}
\end{tikzpicture}
\end{document}
Peter Grill
  • 223,288