# pgfplots: Add/subtract whole columns of data files with one another?

I want to analyze data files with the following format

0.01    1.00    11.00    21.00
0.02    2.00    12.00    22.00
0.03    3.00    13.00    23.00
0.04    4.00    14.00    24.00
0.05    5.00    15.00    25.00


I want to create a plot with pgfplots that uses the first column as the x-values. The y-values shall be obtained by adding several of the other columns to one another or subtract one from the other - a little bit like a spreadsheet application could do it. How can this be achieved with pgfplots? And if it is possible, is it easy to do the same with lines instead of columns or perform other simple mathematical calculations (multiplication, taking the logarithm, etc.)?

• You can use pgfplotstable to read your data and create new colums that contain your math results. There is a large section ans examples available in the documentation – Martin H Jan 13 '12 at 21:30

You can use \addplot table [y expr=...] {<inline table or table macro>}; to specify what to plot. If your columns have names, you can access them in y expr using \thisrow{<column name>}. If you want to use the column index, use \thisrowno{<column number>}. To only have to load the table once instead of supplying it to each plot, use the \pgfplotstableread{<table contents>}<\tablemacro> command.

\documentclass{standalone}

\usepackage{pgfplots}

0.01    1.00    11.00    21.00
0.02    2.00    12.00    22.00
0.03    3.00    13.00    23.00
0.04    4.00    14.00    24.00
0.05    5.00    15.00    25.00
}\datatable

\begin{document}
\begin{tikzpicture}
\begin{axis}[ymin=0, ymax=60]
\addplot table [y expr=\thisrowno{1} + \thisrowno{2} + \thisrowno{3}] {\datatable}; \addlegendentry{$1 + 2 + 3$}
\addplot table [y expr=\thisrowno{2} - \thisrowno{1}] {\datatable}; \addlegendentry{$2-1$}

\end{axis}
\end{tikzpicture}
\end{document}