# Calculate and draw difference of two plots automatically

I'm trying to automatically generate a plot, which visualizes the difference of two other plots. The input source is a single(!) file. The data is filtered by one column with /tr/rowfilter. Here is an example image:

I want that the "difference (b-a)" plot is generated by TikZ/PGFPlots/PGFPlotstable/LaTeX. I've experimented with /pgfplots/x filter, but I wasn't successful (I don't even know, if it's possible) Here is the corresponding tex example:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\pgfkeys{
/tr/rowfilter/.style 2 args={
/pgfplots/x filter/.append code={
\edef\arga{\thisrow{#1}}
\edef\argb{#2}
\ifx\arga\argb
\else
\def\pgfmathresult{}
\fi
}
}
}

\usepackage{filecontents}
\begin{filecontents}{diagram.dat}
x y kind
1 1 a
2 6 a
3 10 a
4 13 a
5 15 a

1 1 b
2 7 b
3 13 b
4 17 b
5 20 b
\end{filecontents}
\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
{\huge The way it is now:}
\vspace{0.5cm}

\begin{tikzpicture}
\begin{axis}[
height=8cm,
width=12cm,
grid=major,
legend pos=outer north east
]
\end{axis}
\end{tikzpicture}
\vspace{1cm}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
{\huge The way it should look like}
\vspace{0.5cm}

\begin{tikzpicture}
\begin{axis}[
height=8cm,
width=12cm,
grid=major,
legend pos=outer north east
]

%%% This should be generated with Tikz/PGFPLOTS/PGFPLOTSTABLE/Latex!!!
(1,0)
(2,1)
(3,3)
(4,4)
(5,5)
};

\end{axis}
\end{tikzpicture}

\end{document}


Can you help me?

-
Welcome to TeX.SE. And thanks for posting a complete MWE. –  Peter Grill Jul 7 '12 at 4:34
Thanks for the compliment:) –  Thomas Rebele Jul 7 '12 at 14:34
See also my proposal for tex.stackexchange.com/questions/62536/… - perhaps that is interesting for you as well. In short: if you can efford a simpler input table format, you can simplify the task drastically. –  Christian Feuersänger Jul 7 '12 at 21:01
Thanks for the comment, but changing the table format means probably more programming in c (or post-processing with some other tools), so the solution with PGFPLOTS is better for me in this case –  Thomas Rebele Jul 13 '12 at 20:14

You can use the stack plot=y functionality, using stack dir=minus to subtract the a plot from the b plot.

When plotting the b plot, add stack plots=y to the options:

\addplot +[stack plots=y] table[/tr/rowfilter={kind}{b}] {diagram.dat};


and then plot the difference by "stacking" the a plot on top, with a negative sign:

\addplot +[stack plots=y, stack dir=minus] table[/tr/rowfilter={kind}{a}] {diagram.dat};


et voilà:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\pgfkeys{
/tr/rowfilter/.style 2 args={
/pgfplots/x filter/.append code={
\edef\arga{\thisrow{#1}}
\edef\argb{#2}
\ifx\arga\argb
\else
\def\pgfmathresult{}
\fi
}
}
}

\usepackage{filecontents}
\begin{filecontents}{diagram.dat}
x y kind
1 1 a
2 6 a
3 10 a
4 13 a
5 15 a

1 1 b
2 7 b
3 13 b
4 17 b
5 20 b
\end{filecontents}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
height=8cm,
width=12cm,
grid=major,
legend pos=outer north east
]

% Make the "b" plot the first "stacked" plot
\addlegendentry{$(b-a)$}