2

I have the following data.dat file, which looks like this

n  1   2  3  4
L1 11  7  13 14
L2 52  61 17 18
L3 9   10 11 12

I want to plot make a plot of L1 (y-axis) against n (x-axis). The same question was asked in this post and was solved, but I don't follow the answer.

Specifically, is there any way to achieve this without adding a default column to the data as suggested in the linked post? I am importing the above data using \pgfplotstableread{data.dat}{\data}

I have the following MWE:

\documentclass[tikz]{standalone}
\usepackage{pgf,pgfplots,pgfplotstable}
\usepackage{filecontents}   

\begin{document} 
\pgfplotstableread{data.dat}{\data}
\begin{tikzpicture}[]
    \begin{axis}[width=\columnwidth,
        height=0.618\columnwidth,
        clip bounding box=upper bound,
        xlabel style = {font=\large, yshift=-0.5ex},
        ylabel style = {font=\large, yshift=-0.5ex},
        xlabel={$n$},
        ylabel={L1},
        \pgfplotstabletranspose[colnames from=n]\datatable{\data},
        ]
        \addplot [black, line width=0.8pt] table [x={colnames}, y={L1}] {\datatable};
\end{tikzpicture}
\end{document}

However this does not run.

1

I may not use the key in the intended way, but according to what I find all you need to do is to

  • use \pgfplotstabletranspose[colnames from=<col>]\datatable{\loadedtable}, where col is the name of the column that you want to use for x, say.
  • in the plot use x=<col>.

That is, <col> gets mapped to colnames and pgfplots can use it as a valid column name.

\documentclass{article}

\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{table}[htb]
\pgfplotstableread{
n  1   2  3  4
L1 11  7  13 14
L2 52  61 17 18
L3 9   10 11 12
}\loadedtable
\pgfplotstabletranspose[colnames from=n]\datatable{\loadedtable}
\centering
\pgfplotstabletypeset[string type]\datatable
\caption{Table.}
\end{table}

\begin{figure}[htb]
\centering\begin{tikzpicture}
    \begin{axis}
      \pgfplotstableread{
        n  1   2  3  4
        L1 11  7  13 14
        L2 52  61 17 18
        L3 9   10 11 12
      }\loadedtable
      \pgfplotstabletranspose[colnames from=n]\datatable{\loadedtable}

      \addplot table[x=colnames,y=L1] {\datatable};
    \end{axis}
  \end{tikzpicture}
\caption{Plot.}  
\end{figure}
\end{document}

enter image description here

It works the same with data files. (Note that there have been some recent changes on filecontents, so if you get errors this may be because now it seems to have become harder to overwrite files. That is, you may have some old file in the directory, which gets loaded and causes errors.)

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{data.dat}
n  1   2  3  4
L1 11  7  13 14
L2 52  61 17 18
L3 9   10 11 12
\end{filecontents*}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{table}[htb]
\pgfplotstableread{data.dat}\loadedtable
\pgfplotstabletranspose[colnames from=n]\datatable{\loadedtable}
\centering
\pgfplotstabletypeset[string type]\datatable
\caption{Table.}
\end{table}

\begin{figure}[htb]
\centering\begin{tikzpicture}
    \begin{axis}
      \pgfplotstableread{data.dat}\loadedtable
      \pgfplotstabletranspose[colnames from=n]\datatable{\loadedtable}

      \addplot table[x=colnames,y=L1] {\datatable};
    \end{axis}
  \end{tikzpicture}
\caption{Plot.}  
\end{figure}
\end{document}
  • How can I edit your code to work with importing data, using \pgfplotstableread{data.dat}{\data} – Sid Oct 15 at 14:03
  • Specifically, see the MWE in the updated question – Sid Oct 15 at 14:13
  • @Sid It works in the same way, please see the update. – Schrödinger's cat Oct 15 at 14:41
  • This doesn't work for me. I am getting a LaTeX warning: Redefining document command \PR with arg. spec. ' >{ \SplitArgument{1}{|} } – Sid Oct 15 at 16:32
  • 1
    Apologies, the code was working fine. I have accepted the answer. The error was the encoding in my input data file. I was generating data from Matlab and somehow, I was getting UTF-8 encoded data in my data.dat file. – Sid Oct 15 at 23:09

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.