# Put label on ycomb

I read data from two files and plot ycomb. I would like to calculate difference between values and put it on top of greater value such as shown in figure. How can I do this?

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}

\begin{tikzpicture}
\begin{axis} [
enlarge y limits=0.5,
]
y      x
3      3
};
y      x
5      3
};
\end{axis}
\end{tikzpicture}

\end{document}


Perhaps a bit roundabout, there might well be more easier methods around, but here is one suggestion. There are some comments in the code indicating what happens, I can add more detail if anything is very unclear.

The filecontents stuff at the start is just to make the example self-contained, you don't need that when you already have the files.

\begin{filecontents*}{data1.dat}
y      x
3      3
6      4
\end{filecontents*}
\begin{filecontents*}{data2.dat}
y      x
5      3
2      4
\end{filecontents*}

\documentclass{standalone}

% read data files to tables

% make two new columns to \dataB
% first column has absolute value of the difference between the y-values in the two files
\pgfplotstablecreatecol[
create col/assign/.code={
\pgfplotstablegetelem{\pgfplotstablerow}{y}\of\dataA
\pgfmathparse{abs(\thisrow{y}-\pgfplotsretval)}
\edef\entry{\pgfmathresult}
\pgfkeyslet{/pgfplots/table/create col/next content}\entry
}
]{diff}{\dataB}
% the second column has the maximum of the two y-values
\pgfplotstablecreatecol[
create col/assign/.code={
\pgfplotstablegetelem{\pgfplotstablerow}{y}\of\dataA
\pgfmathparse{max(\thisrow{y},\pgfplotsretval)}
\edef\entry{\pgfmathresult}
\pgfkeyslet{/pgfplots/table/create col/next content}\entry
}
]{max}{\dataB}

\begin{document}

\begin{tikzpicture}
\begin{axis} [
ymin=0
]

only marks,mark=none, % don't actually plot anything visible
nodes near coords, % add the labels to points
nodes near coords align=above, % place labels above points
point meta=explicit % so it reads from the meta column, instead of using the y-value
] table[x=x,y=max,meta=diff] {\dataB};
\end{axis}
\end{tikzpicture}

\end{document}


When you merge your data tables into one table it is quite easy to add the nodes near coords using a third \addplot (as Torbjørn T. did in his answer).

(The same result could also be achieved by not adding a third "dummy" \addplot command, but then would be more complicated.)

For details please have a look at the comments in the code.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
% combine the data in one table
x y1 y2
3 3  5
}{\data}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
enlarge y limits=0.5,
ycomb,
]
% plot the

% add the nodes near coords
% the plot itself should not be visible
draw=none,
% but we want to have the nodes near coords' ...
nodes near coords,
% ... for which the values are explicitly given/calculated
point meta=explicit,
% only needed in case you want to add another \addplot to not
% change the cycle list index etc.
forget plot,
] table [
x=x,
% add the nodes near coords' at the larger of the two values ...
y expr={max(\thisrow{y1},\thisrow{y2})},
% ... and show the difference of the two values
meta expr={abs(\thisrow{y1}-\thisrow{y2})},
] {\data};
\end{axis}
\end{tikzpicture}
\end{document}
`