13

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

  • 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
18

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}

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.