I am making graphs from a huge CSV with a macro hacked from https://tex.stackexchange.com/a/45125 and https://tex.stackexchange.com/a/24023 Which selects start/end column and adds them etc. But duplicate legend entries have a -index# where # is the column number. Can pgfplots be helped to display legend entries exactly as they are in first line of the csv? I have tried protecting entries with {}. This did not work. It is not really an option to manually add legends.
\documentclass{standalone}
\usepackage{pgfplots,pgfplotstable}
\usepackage{filecontents}
\begin{filecontents}{testdata.dat}
AB;AB+;AB;ABerror;AB+error;ABerror;
1; 2; 3; 0.2; 0.1; 0.1;
2; 3; 4; 0.3; 0.2; 0.2;
3; 4; 5; 0.4; 0.3; 0.3;
4; 5; 6; 0.5; 0.4; 0.4;
\end{filecontents}
\begin{document}
\pgfplotsset{%
table/col sep = semicolon}
\newcommand{\plotfile}[1]{%
\pgfplotstableread{#1}{\table}
\pgfplotstablegetcolsof{#1}
\pgfmathtruncatemacro\numberofcols{\pgfplotsretval - 1}
\pgfplotsinvokeforeach{0,...,\numberofcols}{%
\pgfplotstablegetcolumnnamebyindex{##1}\of{\table}\to{\colname}
\addplot+[] table [x expr=\coordindex,y
index=##1] {#1};
\addlegendentryexpanded{\colname}
}
}
\newcommand{\plotfileLegend}[4]{%
\pgfplotstableread{#1}{\table}
\pgfplotstablegetcolsof{#1}
\pgfmathtruncatemacro\numberofcols{(#3+1-#2)/2}
\pgfmathtruncatemacro\startcol{#2-1}
\pgfmathtruncatemacro\endcol{#3-\numberofcols-1}
\pgfplotsinvokeforeach{\startcol,...,\endcol}{%
\pgfmathtruncatemacro\errorcolumn{##1+\numberofcols}
\addplot table [x expr=\coordindex/#4,y index=##1, y error index = \errorcolumn] {#1};
\pgfplotstablegetcolumnnamebyindex{##1}\of{\table}\to{\colname}
\addlegendentryexpanded{\colname}
}
}
\begin{tikzpicture}
\begin{axis}[xlabel={Influx},
ylabel={Center of mass},
ymode=log,
legend style={
font=\footnotesize,
at={(1.3,1)},
anchor=north east},
error bars/y dir=both,
error bars/y explicit,
error bars/error mark=none,
error bars/error bar style={line width=0.8,solid}]
\plotfileLegend{testdata.dat}{1}{6}{1}
\end{axis}
\end{tikzpicture}
\end{document}
I am still not 100% on how this works. Where are the references to \pgfplotstablegetcolumnnamebyindex in the manuals?
