1

I'm creating grouplots for various datasets. Apart from some y-values, they all share the same x-values. In the past I used something like this to do the task:

\addplot table [x index = 0, y expr = \thisrowno{1}*10 ]{data.txt};

But re-loading the data for every single plot seems overkill to me. I already found out that I can use:

\pgfplotstableread[]{data.txt}\data

to preload and then plot with

\addplot table [x index = 0, y expr = \thisrowno{1}*10 ] from \data;

For the sake of readability I now would like to use something like

\addplot table [x = f1, y = f2 ] from \data;

like I could, if my file would look like:

f1 f2
0 0
1 2
2 4

But the headers of my file are unusable rubbish and I can't get any column names out of it. Also I need to preprocess my data with some factors, so I need \thisrowno{1} or \thisrow{f1}, do I? But I'd like to do all that in advance, before the first addplot - how can I do that?

Something like:

\pgfplotstableread[]{data.txt}\data
f1 = \thisrowno{0}*10 from \data # pseudo code
f2 = \thisrowno{1}*42 from \data # pseudo code

\addplot table [x = f1, y = f2 ] from \data;

I already tried to name my columns with

\pgfplotstableset{columns={f1,f2}}

which gave me no error, but it didn't had any effect neither.


MWE

\documentclass{article}

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

\begin{document}  

    \begin{tikzpicture}

    \pgfplotstableread[skip first n=2]{data.txt}\data

    \begin{axis}
        \addplot table [x index = 0, y expr = \thisrowno{1}*10 ] from \data;
    \end{axis}

    \end{tikzpicture}  

\end{document}

and the data file data.txt

rubbish
rubbish
0 0
1 2
2 4
  • I am not 100% sure if I have understood your problem right. About the "rubbish" headers: How about just commenting these and simply add a new one by hand to the (source) data file? About the preprocessing: Here I guess you should also just do it in an external tool (because this will be much faster and you only have to do it once then) or you could do it with PGFPlotsTable and then save the result to another file which you can then use for your \addplot commands. Was that approximately what you want to achieve? – Stefan Pinnow Feb 18 '17 at 14:38
1

You are looking for \pgfplotstablecreatecol.

enter image description here

\begin{filecontents*}{data.txt}
rubbish
rubbish
0 0
1 2
2 4
\end{filecontents*}

\documentclass{article}

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

\begin{document}  

    \begin{tikzpicture}

      \pgfplotstableread[skip first n=2]{data.txt}\data
      \pgfplotstablecreatecol[create col/expr={\thisrowno{0}}]{f1}\data
      \pgfplotstablecreatecol[create col/expr={\thisrowno{1}*10}]{f2}\data

    \begin{axis}
        \addplot table [x = f1, y = f2] from \data;
    \end{axis}

    \end{tikzpicture}  

\end{document}

enter image description here

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.