# PGFplots with row wise plot with .dat file

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}
\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.

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]
n  1   2  3  4
L1 11  7  13 14
L2 52  61 17 18
L3 9   10 11 12
\centering
\pgfplotstabletypeset[string type]\datatable
\caption{Table.}
\end{table}

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

\end{axis}
\end{tikzpicture}
\caption{Plot.}
\end{figure}
\end{document}


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]
\centering
\pgfplotstabletypeset[string type]\datatable
\caption{Table.}
\end{table}

\begin{figure}[htb]
\centering\begin{tikzpicture}
\begin{axis}

• How can I edit your code to work with importing data, using \pgfplotstableread{data.dat}{\data} – Sid Oct 15 at 14:03
• 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