1

I would like to define different color for group bar chart, below code report error:

\documentclass[varwidth,border=7]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}

\usepgfplotslibrary{fillbetween}
\pgfplotstableread[col sep=comma]{
key,value1,value2
AAA,36.4,40
BBB,92.5,90
CCC,92.6,80
DDD,92.7,50
EEE,98.2,20
FFF,98.3,30
}\mydata
\pgfplotstablegetrowsof{\mydata}
\pgfmathsetmacro{\numrows}{\pgfplotsretval-1}
\pgfplotstablegetcolsof{\mydata}
\pgfmathtruncatemacro{\numcols}{\pgfplotsretval-1}
\newcommand{\colours}{{"00FF00","0000FF","4f81bd","c0504d","9bbb59","8064a2","4bacc6","f79646","0000ff"}}
\begin{document}
\begin{tikzpicture}[font=\tiny,
    brace/.style={decorate, decoration={brace,amplitude=5pt,raise=1pt}},
  ]
    \begin{axis}[
        name=merging,
        xmin = -.8,xmax=\numrows+.5,
        ymin = 0,ymax=180,
        xtick=data,
        xticklabels from table={\mydata}{key},
        xlabel = {keypoints},
        ytick={},
        ylabel = {Timestamp(secs)},
        ybar=0,
        x tick label style={rotate=40,anchor=east},
        xlabel style = {at={(axis description cs:0.5,-0.1)},anchor=north},
        ylabel style = {at={(axis description cs:-0.05,.5)},rotate=0,anchor=south},
        axis lines=left,
        width=8cm,
        height=6cm,
        enlargelimits=0,
      ]
      \foreach \i in {1,...,\numcols} {
        \pgfmathsetmacro{\colour}{\colours[\i-1]}
        \typeout{ \colour }
        \definecolor{mycolor}{HTML}{\colour}
        \addplot [draw=none,fill=mycolor] table [
          x expr=\coordindex,
          y index=\i,
          col sep=comma,
        ] {\mydata};
        \pgfplotstablegetcolumnnamebyindex{\i}\of{\mydata}\to{\colname}
        \addlegendentryexpanded{\colname};
      }
    \end{axis}
\end{tikzpicture}
\end{document}

error:

s been generated with the most recent feature set (\pgfplotsset{compat=1.18}).

 00FF00
Missing character: There is no ; in font nullfont!
 0000FF
Missing character: There is no ; in font nullfont!

! Package xcolor Error: Undefined color `mycolor'.

See the xcolor package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.54     \end{axis}
                   
? x
No pages of output.

The define command looks like not work!

2
  • 1
    You need \pgfplotsinvokeforeach Feb 24 at 16:43
  • @hpekristiansen I gave it a try.
    – JeT
    Feb 24 at 22:57

1 Answer 1

2

applying @hperkrstiansen comment + amend with comments, i get

enter image description here

\documentclass[varwidth,border=7]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}

\usepgfplotslibrary{fillbetween}
\pgfplotstableread[col sep=comma]{
key,value1,value2
AAA,36.4,40
BBB,92.5,90
CCC,92.6,80
DDD,92.7,50
EEE,98.2,20
FFF,98.3,30
}\mydata

\def\mycolors{{"color1","color2"}} 

\definecolor{color1}{RGB}{36,00,133}
\definecolor{color2}{RGB}{17,00,65}

\pgfplotstablegetrowsof{\mydata}
\pgfmathsetmacro{\numrows}{\pgfplotsretval-1}
\pgfplotstablegetcolsof{\mydata}
\pgfmathtruncatemacro{\numcols}{\pgfplotsretval-1}

\begin{document}
\begin{tikzpicture}[font=\tiny,
    brace/.style={decorate, decoration={brace,amplitude=5pt,raise=1pt}},
  ]
    \begin{axis}[
        name=merging,
        xmin = -.8,xmax=\numrows+.5,
        ymin = 0,ymax=180,
        xtick=data,
        xticklabels from table={\mydata}{key},
        xlabel = {keypoints},
        ytick={},
        ylabel = {Timestamp(secs)},
        ybar=0,
        x tick label style={rotate=40,anchor=east},
        xlabel style = {at={(axis description cs:0.5,-0.1)},anchor=north},
        ylabel style = {at={(axis description cs:-0.05,.5)},rotate=0,anchor=south},
        axis lines=left,
        width=8cm,
        height=6cm,
        enlargelimits=0,
        bar width=0.2,
      ]
      \pgfplotsinvokeforeach{1,...,\numcols}{
        \pgfplotstablegetcolumnnamebyindex{#1-1}\of{\mydata}\to{\colname}
        \pgfmathparse{\mycolors[#1-1]} 
        \edef\tempcolor{\pgfmathresult} 
        \addplot+[ybar,fill=\tempcolor] table [ 
          x expr=\coordindex,
          y index=#1,
          col sep=comma,
        ] {\mydata};
        \addlegendentryexpanded{\colname};
      }
    \end{axis}
\end{tikzpicture}
\end{document}
4
  • +1 Nice. It is working, but difficult to say if this is what OP tried to achieve. Feb 25 at 8:06
  • Thanks, is it possible to define the color in a loop? I do not want to do it manually.
    – lucky1928
    Feb 25 at 15:49
  • maybe with something like \def\mycolors{{"color1","color2"}} \definecolor{color1}{RGB}{36,00,133} \definecolor{color2}{RGB}{17,00,65}
    – JeT
    Feb 25 at 21:21
  • @lucky1928 i amend my answer
    – JeT
    Feb 25 at 21:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .