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.

Tohiko
  • 1,789

1 Answers1

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

CarLaTeX
  • 62,716
  • 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 '18 at 16:41
  • @Tohiko You're right, I'll correct, thank you for accepting my answer. – CarLaTeX Feb 27 '18 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 '18 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 01 '18 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 01 '18 at 13:54