1

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?

enter image description here

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
    \begin{axis} [
        enlarge y limits=0.5,
        ]
        \addplot+[ycomb,scatter] table[x=x,y=y]{
            y      x
            3      3
        };
        \addplot+[ycomb,scatter] table[x=x,y=y]{
            y      x
            5      3
        };
    \end{axis}
\end{tikzpicture}

\end{document}
2

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.

enter image description here

\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}
\usepackage{pgfplotstable} % loads pgfplots which loads tikz
\pgfplotsset{compat=newest}

% read data files to tables
\pgfplotstableread{data1.dat}{\dataA}
\pgfplotstableread{data2.dat}{\dataB}

% 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
        ]
        \addplot+[ycomb] table[x=x,y=y]{\dataA};
        \addplot+[ycomb] table[x=x,y=y]{\dataB};

        % use third \addplot to add labels
        \addplot [
           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}
0

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
    \pgfplotstableread{
        x y1 y2
        3 3  5
    }{\data}
\begin{document}
\begin{tikzpicture}
    \begin{axis} [
        enlarge y limits=0.5,
        ycomb,
    ]
        % plot the
        \addplot table [x=x,y=y1] {\data};
        \addplot table [x=x,y=y2] {\data};

        % add the nodes near coords
        \addplot [
            % 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}

image showing the result of above code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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