Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.)?

share|improve this question
1  
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

1 Answer 1

up vote 11 down vote accepted

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}
\usepackage{pgfplotstable} % For \pgfplotstableread

\pgfplotstableread{
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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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