2

I have a whole bunch of data files that contain the output of a measurement program which are all of the same multicolumn structure and that I want to plot in the same way using pgfplots. Some of the plots are not just plotting the data contained in the files but values calculated from several of those columns. I can achieve this by giving the addplot command an option y expr = ... with a mathematical expression to calculate as shown in the MWE below:

\begin{filecontents}{plot1.dat}
        x   alpha   beta
        1   0.5     3
        2   0.3     19
        3   0.7     4
\end{filecontents}
\begin{filecontents}{plot2.dat}
        x   alpha   beta
        1   2       1
        2   4       3
        3   1       18
\end{filecontents}

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
    \addplot+ table[x=x, y expr=\thisrow{alpha}-\thisrow{beta}]{plot1.dat};
    \addplot+ table[x=x, y expr=\thisrow{alpha}-\thisrow{beta}]{plot2.dat};
\end{axis}
\end{tikzpicture}
\end{document}

However, it is quite annoying and error-prone to do this with plots of some 10+ files by just dumb-copying the \addplot command and exchange the file name for every one of them.

I came up with a slightly more elegant solution (thanks to Using \addplot+ and \foreach in pgfplot) using \foreach to iterate through the file names:

\begin{filecontents}{plot1.dat}
        x   alpha   beta
        1   0.5     3
        2   0.3     19
        3   0.7     4
\end{filecontents}
\begin{filecontents}{plot2.dat}
        x   alpha   beta
        1   2       1
        2   4       3
        3   1       18
\end{filecontents}

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
    \foreach \file in {1,2}
        \addplot table[x=x, y expr=\thisrow{alpha}-\thisrow{beta}]{plot\file.dat};
\end{axis}
\end{tikzpicture}
\end{document}

This reduces complexity a bit since I only have to write the y expr = only once per plot, however for several separated plots I still have to copy the expression from one to the other. Therefore my question is if I can somehow define a "function" that I can just write into the options of \begin{axis} or \addplot+ to be applied as coordinate calculation?

2

You could define your functions separately for each file:

\tikzset{%
    declare function={Function1(\x,\y)=(\x-\y);},
    declare function={Function2(\x,\y)=(\x+\y);},
    declare function={Function3(\x,\y)=(\x*\x-\x*\y);},
}

and then invoke the function based on the file name:

\addplot table[x=x, y expr={Function\file(\thisrow{alpha},\thisrow{beta})}]{plot\file.dat};

enter image description here

Code:

\begin{filecontents}{plot1.dat}
        x   alpha   beta
        1   0.5     3
        2   0.3     19
        3   0.7     4
\end{filecontents}
\begin{filecontents}{plot2.dat}
        x   alpha   beta
        1   2       1
        2   4       3
        3   1       18
\end{filecontents}
\begin{filecontents}{plot3.dat}
        x   alpha   beta
        1   0.5     3
        2   0.3     19
        3   0.7     4
\end{filecontents}

\documentclass{standalone}
\usepackage{pgfplots}

\tikzset{%
    declare function={Function1(\x,\y)=(\x-\y);},
    declare function={Function2(\x,\y)=(\x+\y);},
    declare function={Function3(\x,\y)=(\x*\x-\x*\y);},
}


\begin{document}
\begin{tikzpicture}
\begin{axis}
    \foreach \file in {1,2,3}
        \addplot table[x=x, y expr={Function\file(\thisrow{alpha},\thisrow{beta})}]{plot\file.dat};
\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.