2

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. enter image description here

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

\begin{figure}
\centering

\pgfplotstableread{
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

\pgfplotstableread{
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
\addplot[] table[x index=0,y index=0] \first; 
\addplot[] table[x index=0,y index=1] \first;
\addplot[] table[x index=0,y index=2] \first; 
\addplot[] table[x index=0,y index=3] \first;
\addplot[] table[x index=0,y index=4] \first; 
\addplot[] table[x index=0,y index=5] \first;
\addplot[] table[x index=0,y index=6] \first;
\addplot[] table[x index=0,y index=7] \first;
%\coordinate (top) at (rel axis cs:0,1);
\nextgroupplot
\addplot[] table[x index=0,y index=0] \second; 
\addplot[] table[x index=0,y index=1] \second;
\addplot[] table[x index=0,y index=2] \second; 
\addplot[] table[x index=0,y index=3] \second;
\addplot[] table[x index=0,y index=4] \second; 
\addplot[] table[x index=0,y index=5] \second;
\addplot[] table[x index=0,y index=6] \second;
\addplot[] table[x index=0,y index=7] \second;
%\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}

1 Answer 1

3

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.

output of code

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots,colorbrewer}
\usetikzlibrary{matrix}
\begin{document}
\begin{figure}
\centering
%\pgfplotsset{}
\pgfplotstableread{
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

\pgfplotstableread{
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,
        group name=G  % <-- added
    },
    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,
    height=0.5\textwidth, % <-- added, just remove/adjust if you want higher plots
    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}
   \addplot table[x index=0,y index=\i] \first;

\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}
   \addplot table[x index=0,y index=\i] \second;

\end{groupplot}
\path (G c1r1.south west) -- node[rotate=90,above=5mm,anchor=south]   {Empirical prob. of correctly selecting the order} (G c1r2.north west);
\end{tikzpicture}
\caption{}
\label{}
\end{figure}
\end{document}
3
  • Thank you for your reply. The plots look nice, but there are a couple of problems though. The first problem is that there are some bars that go beyond 1, however, since the plot is for probabilities, no bar is supposed to be above 1. The second thing is that since the x indexes are characters not numbers, it would be good if both bar plots show them as characters. Thank you again for your answer. Feb 26, 2018 at 8:27
  • @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
  • 1
    @SaïdMaanan See updated answer, \foreach \i in {1,...,8}, and xticklabels. Feb 26, 2018 at 8:40

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.