# pgfplots: get color setting from table

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}

% 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

}

\end{axis};
\end{tikzpicture}

\end{document}

-

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}

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%
\fi
}
\end{axis};
\end{tikzpicture}

\end{document}


-
Yup, much more elegant! –  Jake Mar 9 '13 at 22:50
@Jake Thanks but I think your answer is more general, and I think it should stay though for the expansion demonstration. Would you mind undeleting it? –  percusse Mar 9 '13 at 22:52
Awesome, thank you both for your quick answers. both works fine for me, yet I don't fully understand why :-) –  Tobi Mar 9 '13 at 23:10

You can retrieve a particular column name using \pgfplotstablegetcolumnnamebyindex. In this case, because the column names are not unique, this macro won't just return a, b, and c, but a-index1, a-index2, etc. To extract only the first character from a macro, you can use the xstring package (see Extract first & last characters of macro argument?). To call styles using macros, you can use Andrew Stacey's approach from How to submit a set of tikz command with pgfkeys?. Note that you'll have to use \pgfplotsinvokeforeach instead of \foreach to make sure that the macros are executed at the proper time:

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

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

\tikzset{
a/.style={
mycolorA
},
b/.style={
mycolorB
},
c/.style={
mycolorC
},
execute style/.style = {#1},
execute macro/.style = {execute style/.expand once=#1},
extract group/.code={
\pgfplotstablegetcolumnnamebyindex#1\of\atable\to\group
\StrLeft{\group}{1}[\firstletter]
}
}

% 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}
\pgfplotsinvokeforeach{1,...,\pgfmathresult}
{% The loop starts here
\addplot [extract group=#1, execute macro=\firstletter] table [x index=0,y index=#1] {\atable};

}

\end{axis};
\end{tikzpicture}

\end{document}

-
Percusse's answer is better –  Jake Mar 9 '13 at 22:50