4

I would like to create a 2 dimensional plot with several lines.

The plot should be normalized by data from first txt-file. I want to devide y-values of all graphs by previously somehow extracted y-values from my first txt-file.

If y-values of different files were named y1, y2 and y3 the plotted y-values should be y1/y1, y2/y1 and y3/y1.

Since I have to make several of these plots it would be good if it could be "automated" as much as possible.

Thanks in advance!

MWE:

    \documentclass[final]{scrreprt}
    \usepackage{filecontents}
    \usepackage{pgfplots}
    \pgfplotsset{compat=newest}

    \begin{filecontents*}{x_y_values1.txt}
    x y
    1 1
    2 2
    3 3
    \end{filecontents*}

    \begin{filecontents*}{x_y_values2.txt}
    x y
    1 2
    2 3
    3 4
    \end{filecontents*}

    \begin{filecontents*}{x_y_values3.txt}
    x y
    1 3
    2 4
    3 5
    \end{filecontents*}

    \begin{document}

    \begin{figure}

    \pgfplotsset{width=\textwidth}

    \begin{tikzpicture}
    \begin{axis}[]
        \addplot[] % y-values should be devided by y-value from x_y_values1.txt
            table [col sep=space,x expr=\thisrow{x},y expr=\thisrow{y}] 
                    {x_y_values1.txt};
        \addplot[] % y-values should be devided by y-value from x_y_values1.txt
            table [col sep=space,x expr=\thisrow{x},y expr=\thisrow{y}] 
                        {x_y_values2.txt};
        \addplot[] % y-values should be devided by y-value from x_y_values1.txt
            table [col sep=space,x expr=\thisrow{x},y expr=\thisrow{y}] 
                        {x_y_values3.txt};
    \end{axis}
    \end{tikzpicture}
    \end{figure}

    \end{document}
  • In the final version, will these files be generated externally? – John Kormylo Aug 16 '16 at 13:47
  • Yes, the files will be generated externally. Due to the large number of plots and files it would be easiest, if its possible to make this little calculation somehow internal. – Chris Aug 16 '16 at 14:16
1

Using pgfplotstable, I copied the y column from the first file/table into every other file/table. The \pgfplotstableclear isn't really needed; I just feel better using it.

 \documentclass[final]{scrreprt}
    \usepackage{filecontents}
    \usepackage{pgfplots}
    \pgfplotsset{compat=newest}
    \usepackage{pgfplotstable}

    \begin{filecontents*}{x_y_values1.txt}
    x y
    1 1
    2 2
    3 3
    \end{filecontents*}

    \begin{filecontents*}{x_y_values2.txt}
    x y
    1 2
    2 3
    3 4
    \end{filecontents*}

    \begin{filecontents*}{x_y_values3.txt}
    x y
    1 3
    2 4
    3 5
    \end{filecontents*}

  \begin{document}

    \begin{figure}
    \pgfplotstableread[col sep=space]{x_y_values1.txt}\firsttable

    \pgfplotsset{width=\textwidth}

    \begin{tikzpicture}
    \begin{axis}[]
      \foreach \filename in {x_y_values1.txt, x_y_values2.txt, x_y_values3.txt} {%
        \pgfplotstableread[col sep=space]{\filename}\thistable
        \pgfplotstablecreatecol[create col/copy column from table=\firsttable{y}]{y scale}\thistable
        \addplot[] table [x=x, y expr={\thisrow{y} / \thisrow{y scale}}] \thistable;
        \pgfplotstableclear\thistable};
    \end{axis}
    \end{tikzpicture}
    \end{figure}
\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.