4

Is there a way to read a table as rows instead of columns? Like the MWE example below illustrates:

\begin{filecontents*}{line123.dat}
    x1  y1  y2      x3      y3
    0   1   2       2       1
    1   2   5       4.1     2
    2   3   5       6.1     3
    4   5   3       8.1     5
    7   9   1       10.1    9
\end{filecontents*}
% I prefer my format to be as follows:
% \begin{filecontents*}{transposed_line123.dat}
%     x1 0 1 2 4 7
%     y1 1 2 3 5 9
%     y2 2 5 5 3 1
%     x3 2 4.1 6.1 8.1 10.1
%     y3 1 2 3 5 9
% \end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}
        \addplot table [x=x1, y=y1] {line123.dat};
        \addplot table [x=x1, y=y2] {line123.dat};
        \addplot table [x=x3, y=y3] {line123.dat};
    \end{axis}
\end{tikzpicture}
\end{document}

The reason is because I have more columns that rows in line123.dat.

3
  • Hi, Have you forgotten the\documentclass that you have used into the your preamble.
    – Sebastiano
    Feb 27, 2018 at 14:28
  • 1
    I don't believe I have. It's right after the filecontents* environments.
    – Tohiko
    Feb 27, 2018 at 14:30
  • 1
    @Sebastiano Look carefully at the MWE, the \documentclass is present.
    – CarLaTeX
    Feb 27, 2018 at 15:16

1 Answer 1

3

You could use \pgfplotstabletranspose:

\begin{filecontents*}{transposed_line123.dat}
     x1 0 1 2 4 7
     y1 1 2 3 5 9
     y2 2 5 5 3 1
     x3 2 4.1 6.1 8.1 10.1
     y3 1 2 3 5 9
\end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.14} 

\begin{document}
\pgfplotstabletranspose[input colnames to={x1},colnames from={x1}]{\mytabletoplot}{transposed_line123.dat}

\begin{tikzpicture}
    \begin{axis}
        \addplot table [x=x1, y=y1] {\mytabletoplot};
        \addplot table [x=x1, y=y2] {\mytabletoplot};
        \addplot table [x=x3, y=y3] {\mytabletoplot};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

5
  • Thanks! I also found out that there is no need to load the "untransposed" table. \pgfplotstabletranspose can take the filename as the last input.
    – Tohiko
    Feb 27, 2018 at 16:41
  • @Tohiko You're right, I'll correct, thank you for accepting my answer.
    – CarLaTeX
    Feb 27, 2018 at 16:42
  • Mmm.. For some reason when the data is like this: x1 0 1 2 nan nan \n y1 1 2 3 5 9 Then pgfplots gives a warning: Table 'transposed_line123.dat' has non-unique column name 'nan'. Only the first occurence can be accessed via column names. I am guessing this is a bug. It's not major, but I like my latex documents warning free :) Any idea if this can be circumvented?
    – Tohiko
    Feb 28, 2018 at 17:27
  • \n is just my way of saying there should be a new line there. If I compile the example that you include I get: Package pgfplots Warning: Table 'transposedwithnan.dat' has non-unique column n ame 'nan'. Only the first occurence can be accessed via column names. on input line 9.
    – Tohiko
    Mar 1, 2018 at 13:43
  • 1
    @Tohiko Yes, but it's only a warning, the plot in not influenced, see this: \begin{filecontents*}{transposedwithnan.dat} x1 0 1 2 nan nan 4 y1 1 2 3 5 9 10 \end{filecontents*} \documentclass[border=5pt]{standalone} \usepackage{booktabs} \usepackage{pgfplotstable} \pgfplotsset{compat=1.14} \begin{document} \pgfplotstabletranspose[input colnames to={x1},colnames from={x1}]\mytabletoplot{transposedwithnan.dat} \begin{tikzpicture} \begin{axis} \addplot table [x=x1, y=y1] {\mytabletoplot}; \end{axis} \end{tikzpicture} \end{document}
    – CarLaTeX
    Mar 1, 2018 at 13:54

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.