4

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?

SpmP
  • 1,201

1 Answers1

1

If you look at the warnings that pgfplots throw at you, it detects that there are duplicate col names and tries to help you to give them alternative names by postfixing --index[<duplicate number>]. So that is expected. However you can truncate the name to be displayed in the legend entry.

I used xstring in a lazy fashion to see whether the double dash exist in the name then truncate accordingly. Also, I modified the macro a little such that the second entry is now a \foreach array such that you can selectively plot them. And the third entry is the number of cols to skip to reach the error column of that particular column.

The documentation is in the pgfplotstable manual.

\documentclass{standalone}
\usepackage{pgfplotstable,xstring,filecontents}
\pgfplotsset{compat=1.10,table/col sep = semicolon}

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

\newcommand{\plotfileLegend}[4]{%
    \pgfplotstableread{#1}{\table}
    \pgfplotsinvokeforeach{#2}{%
    \pgfmathtruncatemacro\errorcolumn{##1 + #3}
    \addplot table [x expr=\coordindex/#4,y index=##1, y error index = \errorcolumn] {\table};
    \pgfplotstablegetcolumnnamebyindex{##1}\of{\table}\to{\colname}
    \StrBefore{\colname}{--}[\mycolname]
    \addlegendentryexpanded{\ifx\empty\mycolname\relax\colname\else\mycolname\fi}
    }
}
\begin{document}
\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}{0,...,2}{3}{1}
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

percusse
  • 157,807