[Tex/LaTex] pgfplots: get color setting from table

pgfplotspgfplotstabletikz-pgf

I want to plot multiple lines using pgfplots.

Each of the lines should have a color, depending on the group it belongs to.

In the example attached, a table is provided, containing all the coordinates for the lines and additional information which should be used to decide which color to use

I tried a few things, but obviously I'm too unexperienced with LATEX/pgfplots.

It would be cool if someone could give me a hint,

Thanks, Tobi

\documentclass{article}  
\usepackage{pgfplots}    
\begin{document}  

\pgfplotstableread{  
% This table holds the coordinates for multiple lines sharing the same x-values.
% The x-values are provided in the first column, the y-values in the subsequent columns.
% The character in the first row indicates the group the line belongs to
t   a   a   a   b   b   c   c   c   c  
-1  0   -2  -1  1   0   2   2   1   1  
0   0   0   0   0   0   0   0   0   0  
1   3   2   4   9   8   6   7   9   5  
2   4.5 3   6   13  12  9   10  12  8  
3   5.8 3.5 7   15  14  11  12  13  9  
4   5.1 3.3 6.5 14  13  10  11  12  8.5  
5   5   3.1 6.4 14  13  10  11  12  8.4  
}\atable  

% These are the colors I want to use for the 3 groups (a,b,c)  
\definecolor{mycolorA}{named}{red}  
\definecolor{mycolorB}{named}{green}  
\definecolor{mycolorC}{named}{blue}  

% Ok, let's plot the data  
\begin{tikzpicture}  
    \begin{axis}[no markers]  

        % Loop over columns present (If someone knows a shorter way to do this please let me know)  
        \pgfplotstablegetcolsof{\atable}  
        \pgfmathparse{\pgfplotsretval-1}  
        \foreach \i in {1,...,\pgfmathresult} 
        {% The loop starts here       

            % Here somehow the character in the first row  has to be read out.  
            % if the character in the first row of the current column \i 
            % is equal to 'a' then use mycolorA, if 'b' use mycolorB etc  
            \addplot table[x index=0,y index=\i] {\atable};  

        }  

    \end{axis};  
\end{tikzpicture}  

\end{document}  

Best Answer

You can also do things per column via \pgfplotstableforeachcolumn loop where you get the column name and the index as macros and you avoid some of the code. I've used xstring package as Jake mentioned but I still think that this is not a good structure for keeping track of styles. I would recommend using custom cycle lists.

\documentclass{article}  
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\usepackage{xstring}
\begin{document}  

\pgfplotstableread{  
t   a   a   a   b   b   c   c   c   c  
-1  0   -2  -1  1   0   2   2   1   1  
0   0   0   0   0   0   0   0   0   0  
1   3   2   4   9   8   6   7   9   5  
2   4.5 3   6   13  12  9   10  12  8  
3   5.8 3.5 7   15  14  11  12  13  9  
4   5.1 3.3 6.5 14  13  10  11  12  8.5  
5   5   3.1 6.4 14  13  10  11  12  8.4  
}\atable  


\begin{tikzpicture}  
    \begin{axis}[no markers,a/.style={red},b/.style={green},c/.style={blue}]  

\pgfplotstableforeachcolumn\atable\as\colname{%
\StrLeft{\colname}{1}[\mycolname]
\ifnum\pgfplotstablecol=0\else%
\expandafter\addplot\expandafter[\mycolname] table[x=t,y index=\pgfplotstablecol] {\atable};
\fi
}
    \end{axis};  
\end{tikzpicture}  

\end{document}  

enter image description here