# PGFPLOTS - Grouped bar plots with different number of groups

I would like to create grouped bar plots using pgfplots. The result I would like to have is similar to the figures below, but one on top of the other instead of next to each other.

I tried the following code, but it's not giving me what I am looking for:

\begin{figure}
\centering

0 0         0.2857    0         0         0         0.4286    0         0
1 0.7143    0.8571    0.7143    0         0.7143    0.8571    0.7143    0
2 0.7143    0.2857    0.5714    0.1429    0.7143    0.2857    0.5714         0.1429
3 0         0         0         0.8571    0         0         0         0.8571
}\first

0   0         0.1429         0         0         0         0.1429            0         0
1   0         0.2857         0         0         0         0.4286         0         0
2   0         0.7143         0         0         0         0.7143         0         0
3   1.0000    0.2857         1.0000    0.1429    1.0000    0.2857         1.0000    0.1429
4   0         0              0         0.8571    0         0              0         0.8571
}\second

\begin{tikzpicture}
\begin{groupplot}[
group style={group size = 1 by 2, xticklabels at=edge bottom},
ymin=0,
axis on top,
ybar=1pt,
xtick=data,
enlarge x limits=0.2,
every axis plot/.append style={fill},
cycle list name = Paired,
tick label style={font=\footnotesize},
width = 0.8\textwidth
]
\nextgroupplot
%\coordinate (top) at (rel axis cs:0,1);
\nextgroupplot
%\coordinate (bot) at (rel axis cs:1,0);
\end{groupplot}
%\path (top-|current bounding box.west)--node[anchor=south,rotate=90]   {Empirical prob. of correctly selecting the order} (bot-|current bounding box.west);
%%Legend
%\path (top|-current bounding box.north)--coordinate(legendpos) (bot|-current bounding box.north);
%\matrix[ matrix of nodes, anchor=south, draw, inner sep=0.2em, draw]
%at([yshift=1ex, xshift=-7.5ex]legendpos)
%  {
%    \ref{SBC}&SBC&[5pt]
%    \ref{FPE}&FPE&[5pt]
%    \ref{RNML}&RNML&[5pt]
%    \ref{AIC}&AIC&[5pt]
%    \ref{AICc}&$\mathrm{AIC_{c}}$&[5pt]
%    \ref{KIC}&KIC&[5pt]
%    \ref{KICc}&KIC&[5pt]
%    \ref{KICd}&$\mathrm{KIC_{c}}$&\\};
\end{tikzpicture}
\caption{}
\label{}
\end{figure}


There are a couple of problems with your code. First of all, when you do \addplot [] that means that any settings from the active cycle list are ignored, and you end up with all black bars. You can append settings to those of the cycle list if you do \addplot +[...], but here you can just remove the [] from each \addplot.

Further, the default bars are too wide, so you can't fit four groups of eight bars inside the given axis width. Reducing the bar width solves this.

You can use xticklabels={foo,bar,baz,etc} to set the tick labels. You might have to set this separately for each groupplot, as you have four rows in the first data file, and five in the second.

I also simplified the code a bit, by using \foreach to add the plots, and I added the legend to the first axis in the normal way, instead of using \label/\ref, as that didn't seem necessary.

And I showed an alternative method for adding the y-label for the axes.

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots,colorbrewer}
\usetikzlibrary{matrix}
\begin{document}
\begin{figure}
\centering
%\pgfplotsset{}
0 0         0.2857    0         0         0         0.4286    0         0
1 0.7143    0.8571    0.7143    0         0.7143    0.8571    0.7143    0
2 0.7143    0.2857    0.5714    0.1429    0.7143    0.2857    0.5714         0.1429
3 0         0         0         0.8571    0         0         0         0.8571
}\first

0   0         0.1429         0         0         0         0.1429            0         0
1   0         0.2857         0         0         0         0.4286         0         0
2   0         0.7143         0         0         0         0.7143         0         0
3   1.0000    0.2857         1.0000    0.1429    1.0000    0.2857         1.0000    0.1429
4   0         0              0         0.8571    0         0              0         0.8571
}\second

\begin{tikzpicture}
\begin{groupplot}[
group style={
group size = 1 by 2,
},
ymin=0,
ymax=1, % makes sense to add this I suppose
axis on top,
ybar=1pt,
xtick=data,
enlarge x limits={abs=0.5},
every axis plot/.append style={fill},
cycle list/Paired,
cycle list name = Paired,
tick label style={font=\footnotesize},
width = 0.8\textwidth,
every axis/.append style={
bar width=4pt % <-- reduce width of bars
}
]
\nextgroupplot[
xticklabels={a,b,c,d,e},
legend style={
at={(0.5,1.05)},
anchor=south,
legend columns=-1
}]
\foreach \i in {1,...,8}

\legend{SBC,FPE,RNML,AIC,AIC\textsubscript{c},KIC,KICc,KICd}

\nextgroupplot[
xticklabels={a,b,c,d,e} % replace a, b, c etc. with whatever labels you need
]
\foreach \i in {1,...,8}

• @SaïdMaanan Well, you are the one who plotted x index=0 vs. y index=0, in that sense I just copied what you did. Start the loop at 1 instead of 0, will fix in a bit. Feb 26, 2018 at 8:28
• @SaïdMaanan See updated answer, \foreach \i in {1,...,8}, and xticklabels. Feb 26, 2018 at 8:40