Why X_Group, Y_Group does not appear in this pgfplot example? I tried to change the below reference slightly, but my example does not show the grouping as in the example.
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\makeatletter
\newcounter{groupcount}
\pgfplotsset{
draw group line/.style n args={5}{
after end axis/.append code={
\setcounter{groupcount}{0}
\pgfplotstableforeachcolumnelement{#1}\of\datatable\as\cell{%
\def\temp{#2}
\ifx\temp\cell
\ifnum\thegroupcount=0
\stepcounter{groupcount}
\pgfplotstablegetelem{\pgfplotstablerow}{[index]0}\of\datatable
\coordinate [yshift=#4] (startgroup) at (axis cs:\pgfplotsretval,0);
\else
\pgfplotstablegetelem{\pgfplotstablerow}{[index]0}\of\datatable
\coordinate [yshift=#4] (endgroup) at (axis cs:\pgfplotsretval,0);
\fi
\else
\ifnum\thegroupcount=1
\setcounter{groupcount}{0}
\draw [
shorten >=-#5,
shorten <=-#5
] (startgroup) -- node [anchor=north] {#3} (endgroup);
\fi
\fi
}
\ifnum\thegroupcount=1
\setcounter{groupcount}{0}
\draw [
shorten >=-#5,
shorten <=-#5
] (startgroup) -- node [anchor=north] {#3} (endgroup);
\fi
}
}
}
\makeatother
\pgfplotstableread{
1 66.96 66.88 1
2 66.80 66.88 1
3 67.05 66.88 1
4 67.01 66.88 1
5 66.90 66.88 2
6 66.95 66.88 2
7 66.85 66.88 2
8 66.82 66.88 2
}\datatable
\makeatletter
\begin{tikzpicture}
\begin{axis}[
ylabel=Accuracy,
xtick=data,
xticklabels={M1,M2,M3,M4,M5,M6,M7,M8},
enlarge y limits=false,
enlarge x limits=0.1,
ymin=66.7,ymax=67.1,
ybar stacked,
bar width=10pt,
legend style={
font=\normalsize,
cells={anchor=west},
legend columns=2,
at={(0.3,-0.20)},
anchor=north,
/tikz/every even column/.append style={column sep=0.2cm}
}
]
\addplot table[x index=0,y index=1] \datatable;
\legend{Stacking Models}
\end{axis}
\begin{axis}[
ticks=none,
% xtick=data,
% xticklabels={M1,M2,M3,M4,M5,M6,M7,M8},
enlarge y limits=false,
enlarge x limits=0.1,
ymin=66.7,ymax=67.1,
legend style={
font=\normalsize,
cells={anchor=west},
legend columns=2,
at={(0.71,-0.20)},
anchor=north,
/tikz/every even column/.append style={column sep=0.2cm}
},
draw group line={[index]2}{1}{X\_Group}{-3.5ex}{7pt},
draw group line={[index]2}{2}{Y\_Group}{-3.5ex}{7pt}
]
\addplot table[x index=0,y index=2] \datatable;
\legend{XGBoost}
\end{axis}
\end{tikzpicture}
\end{document}
Below barplot is the current compiled result, and I added the desired result (X_Group, Y_Group should be added).

I tried to change draw group line={[index]2} to draw group line={[index]3} since the grouping information is in the third column of the table, but it only shows the grouping by missing the previous barplot as in below image :(
draw group line={[index]3}{1}{X\_Group}{-3.5ex}{7pt},
draw group line={[index]3}{2}{Y\_Group}{-3.5ex}{7pt}


\makeatletterabove\begin{tikzpicture}should be a\begin{document}. – Dec 30 '18 at 15:34