2

I am trying to put four bar charts within one groupplot. My file is

\documentclass[tikz,border=3pt]{standalone}
\usepackage{xcolor}
\usepackage{pgfplots,pgfplotstable}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.4}
\pgfplotsset{every non boxed x axis/.append style={x axis line style=-},
every non boxed y axis/.append style={y axis line style=-}}
\renewcommand{\rmdefault}{phv}
 % Arial font
\renewcommand{\sfdefault}{phv} 
% Arial font
\definecolor{MediumSeaGreen}{rgb}{0.235294,0.701961,0.443137}
\definecolor{gold}{rgb}{1.000000,0.843137,0.000000}
\definecolor{LightBlue}{rgb}{0.678431,0.847059,0.901961}\definecolor{LightGrey}{rgb}{0.827451,0.827451,0.827451}
\definecolor{DarkGrey}{rgb}{0.662745,0.662745,0.662745}\definecolor{DarkGrey}{rgb}{0.662745,0.662745,0.662745}
\makeatletter
\pgfplotsset{/pgfplots/flexible xticklabels from table/.code n args={3}{%
        \pgfplotstableread[#3]{#1}\coordinate@table
        \pgfplotstablegetcolumn{#2}\of{\coordinate@table}\to\pgfplots@xticklabels
        \let\pgfplots@xticklabel=\pgfplots@user@ticklabel@list@x}}
\makeatother
\pgfplotscreateplotcyclelist{mycolor}{
       {black,fill=MediumSeaGreen,mark=none},
    {black,fill=gold,mark=none},
    {black,fill=LightBlue,mark=none},
    {black,fill=LightGrey,mark=none},
}
%
\begin{document}
\color{DarkGrey}
\pgfplotsset{ tick label style = {font=\tiny}, every axis/.append style={font=\tiny}}
\begin{tikzpicture}
\pgfplotstableread[col sep=space,row sep=crcr]{data.txt}\mydata;
 \begin{groupplot}[
        group style={group size=2 by 2,vertical sep=1.5cm},
        width=8cm,
        height=5cm,  
        major x tick style = transparent,
     yticklabel={\ifdim\tick pt=0pt 0 \else\pgfmathprintnumber{\tick}\fi},
     y tick label style={/pgf/number format/fixed,/pgf/number format/precision=1,
     ylabel near ticks,
     yticklabel pos=left,
     /pgf/number format/fixed zerofill},
     ybar=0pt,
     cycle list name=mycolor,
        ymajorgrids=false,
        ylabel = {\%},   
     xtick={0,1,2,3,4,5,6,7,8,9,10,11},
     flexible xticklabels from table={data}{month}{col sep=space}, 
        x tick label style={rotate=270,anchor=west},
        scaled y ticks = false,
        enlarge x limits=0.05,
        ymin=0,
     ymax=.6]
%    
\nextgroupplot[legend to name=grouplegend,]
 \pgfplotsinvokeforeach {1,...,4}{\addplot[bar width=2pt] table [x expr={\coordindex}, y index=#1] {\mydata};}
% 
\nextgroupplot[legend to name=grouplegend,]
%
\pgfplotsinvokeforeach {1}{\addplot[bar width=2pt,color=black,fill=MediumSeaGreen] table [x expr={\coordindex}, y index=#1] {\mydata};}
\pgfplotsinvokeforeach {2}{\addplot[bar width=2pt,color=black,fill=gold] table [x expr={\coordindex}, y index=#1] {\mydata};}
\pgfplotsinvokeforeach {3}{\addplot[bar width=2pt,color=black,fill=LightBlue] table [x expr={\coordindex}, y index=#1] {\mydata};}
\pgfplotsinvokeforeach {4}{\addplot[bar width=2pt,color=black,fill=LightGrey] table [x expr={\coordindex}, y index=#1] {\mydata};}
%
\nextgroupplot
%
\pgfplotsinvokeforeach {1}{\addplot[bar width=2pt,color=black,fill=MediumSeaGreen] table [x expr={\coordindex}, y index=#1] {\mydata};}
\pgfplotsinvokeforeach {2}{\addplot[bar width=2pt,color=black,fill=gold] table [x expr={\coordindex}, y index=#1] {\mydata};}
\pgfplotsinvokeforeach {3}{\addplot[bar width=2pt,color=black,fill=LightBlue] table [x expr={\coordindex}, y index=#1] {\mydata};}
\pgfplotsinvokeforeach {4}{\addplot[bar width=2pt,color=black,fill=LightGrey] table [x expr={\coordindex}, y index=#1] {\mydata};}
%
\nextgroupplot[legend to name=grouplegend,]
%
\pgfplotsinvokeforeach {1}{\addplot[bar width=2pt,color=black,fill=MediumSeaGreen] table [x expr={\coordindex}, y index=#1] {\mydata};}
\pgfplotsinvokeforeach {2}{\addplot[bar width=2pt,color=black,fill=gold] table [x expr={\coordindex}, y index=#1] {\mydata};}
\pgfplotsinvokeforeach {3}{\addplot[bar width=2pt,color=black,fill=LightBlue] table [x expr={\coordindex}, y index=#1] {\mydata};}
\pgfplotsinvokeforeach {4}{\addplot[bar width=2pt,color=black,fill=LightGrey] table [x expr={\coordindex}, y index=#1] {\mydata};}
%
  \end{groupplot}
\end{tikzpicture}
\end{document}

and data input file is

month   a   b   c   d   
OCT'12  0.62    0.02    3.48    0.22
NOV'12  0.70    0.00    3.58    0.19
DEC'12  0.37    0.00    2.21    0.20
JAN'13  0.37    0.18    3.41    0.25
FEB'13  0.29    1.09    4.04    0.18
MAR'13  0.10    1.12    3.54    0.26    
APR'13  0.00    0.60    3.50    0.18
MAY'13  0.21    0.37    3.50    0.18
JUN'13  0.07    0.37    2.58    0.14
JUL'13  0.00    0.42    3.25    0.02
AUG'13  0.00    0.37    4.62    0.12
SEP'13  0.00    0.48    3.94    0.15

My first question is: If I set the bar width within the braces following \begin{group plot} I get an error because bar width is not recognized by group plot, so I have to set the value whenever I invoke every plot within the group. And my second question relates to the bar colors, invoking the color cycle I have defined in the preamble (\mycolor) with cycle list name=mycolor the colors are not recognized (look at the plot in the upper left corner, all bars are white, the pony way I have found to solve this is invoking each color for each bar as I have done in the other three plots, I am sure there is an easiest way to do this

  • Welcome at Tex.sx! We would appreciate it if you could provide a complete minimal working example, including any referenced content files or 3rd party styles. – Christian Feuersänger Oct 27 '13 at 18:10
  • It also seems as if you have two question in one: the first referring to the bar width and the second appears to be "how can I draw individually colored bars?". It would help you and others if you would make two separate questions, each with its own answer. – Christian Feuersänger Oct 27 '13 at 18:11
2

Regarding your first question, you can set the bar width after the \begin{group plot} by specifying /pgf/bar width=2pt.

Regarding your second question, you need to replace your \addplot[...] commands with \addplot+[...] (add the + sign). With \addplot+[...], options within the brackets are added to the currently set options in the groupplot style, without the + they replace these options.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.