[Tex/LaTex] grouped bar colors in pgfplots

bar chartcolorpgfplots

I have a simple grouped bar chart but would like to have all group bars the same color. Currently each bar in a group is colored, possibly because of the way I have used addplot. I've tried to use a pgfplotsset list but this still changes each bar color, rather than the group. What I am after is all bars in black to be a single color, all in brown a single color, etc. Any suggestions welcomed!

what i have now

![\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pagestyle{empty}
\begin{document}
\pgfplotsset{
/pgfplots/bar cycle list/.style={/pgfplots/cycle list={%
{blue,fill=blue,mark=none},%
{red,fill=red,mark=none},%
{brown!60!black,fill=brown!30!white,mark=none},%
{black,fill=gray,mark=none},
{fill=orange},
{fill=green},
{fill=olive},%
}
},
}
\begin{tikzpicture}
\begin{axis}\[
    ybar=1pt,
    bar width=6pt,
    enlargelimits=0.15,
    legend style={at={(0.5,-0.15)},
      anchor=north,legend columns=-1},
    ylabel={TWh},
    symbolic x coords={black,brown,gas,hydro,other},
    nodes near coords align={vertical},
    \]
\addplot coordinates {(black,118.4) (brown,56.3) (gas,17.5) (hydro,11.5) (other,4.3 )};
\addplot coordinates {(black,112.9) (brown,56.4) (gas,23.4) (hydro,13.4) (other,5.0)};
\addplot coordinates {(black,107.4) (brown,54.3) (gas,23.2) (hydro,14.7) (other,6.4)};
\addplot coordinates {(black,103.4) (brown,52.9) (gas,23.9) (hydro,14.6) (other,7.1)};
\addplot coordinates {(black,99.7) (brown,47.7) (gas,23.6) (hydro,16.9) (other,8.5)};
\legend{black, brown, gas, hydro, other}
\end{axis}
\end{tikzpicture}
\end{document}]

Best Answer

Here is a suggestion using a table. Each group gets its own column. Then the table is ploted column by column that means group by group.

\documentclass[margin=10pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\pgfplotsset{
  bar cycle list/.style={
    cycle list={%
      {blue,fill=blue,mark=none},%
      {red,fill=red,mark=none},%
      {brown!60!black,fill=brown!30!white,mark=none},%
      {black,fill=gray,mark=none},
      {fill=orange},
      {fill=green},
      {fill=olive},%
    }
  },
}

\pgfplotstableread{
black brown gas hydro other
118.4 56.3 17.5 11.5 4.3
112.9 56.4 23.4 13.4 5.0
107.4 54.3 23.2 14.7 6.4
103.4 52.9 23.9 14.6 7.1
 99.7 47.7 23.6 16.9 8.5
}{\mydata}

\pgfplotstablegetrowsof{\mydata}
  \edef\rownumber{\pgfmathresult}
\pgfmathparse{(\rownumber-1)/2}
  \edef\firsttick{\pgfmathresult}
\pgfmathparse{\firsttick+\rownumber+1}
  \edef\secondtick{\pgfmathresult}

\pgfplotstablegetcolsof{\mydata}
  \pgfmathtruncatemacro\colmax{\pgfmathresult-1}

\xdef\mylist{}
  \pgfplotstableforeachcolumn{\mydata}\as{\col}{\xdef\mylist{\mylist"\col",}}
\newcommand\mygrouplist{{\mylist}}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar=-5pt,% ybar must be -bar width
    bar width=5pt,
    enlarge x limits={0.08},
    enlarge y limits={0.15},
    legend style={at={(0.5,-0.15)},
      anchor=north,legend columns=-1},
    ylabel={TWh},
    xtick style={draw=none},
    xtick={\firsttick,\secondtick, ...,1000},
    xticklabel={\pgfmathparse{\mygrouplist[\ticknum]}\pgfmathresult},
    ]

\pgfplotsinvokeforeach{0,...,\colmax}{
  \pgfmathparse{\mygrouplist[#1]}\edef\columnname{\pgfmathresult}
  \addplot table [x expr=\coordindex+#1*(\rownumber+1),y=\columnname,meta expr=\coordindex+1]{\mydata};
  \addlegendentryexpanded{\columnname}
  }
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

It is also possible to plot only selected columns/groups:

\documentclass[margin=10pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\pgfplotsset{
  bar cycle list/.style={
    cycle list={%
      {blue,fill=blue,mark=none},%
      {red,fill=red,mark=none},%
      {brown!60!black,fill=brown!30!white,mark=none},%
      {black,fill=gray,mark=none},
      {fill=orange},
      {fill=green},
      {fill=olive},%
    }
  },
}

\pgfplotstableread{
black brown gas hydro other
118.4 56.3 17.5 11.5 4.3
112.9 56.4 23.4 13.4 5.0
107.4 54.3 23.2 14.7 6.4
103.4 52.9 23.9 14.6 7.1
 99.7 47.7 23.6 16.9 8.5
}{\mydata}

\pgfplotstablegetrowsof{\mydata}
  \edef\rownumber{\pgfmathresult}
\pgfmathparse{(\rownumber-1)/2}
  \edef\firsttick{\pgfmathresult}
\pgfmathparse{\firsttick+\rownumber+1}
  \edef\secondtick{\pgfmathresult}

%\pgfplotstablegetcolsof{\mydata}
  %\pgfmathtruncatemacro\colmax{\pgfmathresult-1}
\newcommand\colmax{2}

%\xdef\mylist{}
  %\pgfplotstableforeachcolumn{\mydata}\as{\col}{\xdef\mylist{\mylist"\col",}}
%\newcommand\mygrouplist{{\mylist}}
\newcommand\mygrouplist{{"black","gas","hydro"}}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar=-8pt,% ybar=-bar width
    bar width=8pt,
    enlarge x limits={0.08},
    enlarge y limits={0.15},
    legend style={at={(0.5,-0.15)},
      anchor=north,legend columns=-1},
    ylabel={TWh},
    xtick style={draw=none},
    xtick={\firsttick,\secondtick, ...,1000},
    xticklabel={\pgfmathparse{\mygrouplist[\ticknum]}\pgfmathresult}
    ]

\pgfplotsinvokeforeach{0,...,\colmax}{
  \pgfmathparse{\mygrouplist[#1]}\edef\columnname{\pgfmathresult}
  \addplot table [x expr=\coordindex+#1*(\rownumber+1),y=\columnname]{\mydata};
  \addlegendentryexpanded{\columnname}
  }
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here