2

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.

Reference: https://tex.stackexchange.com/questions/166769/line-graph-superimposed-on-grouped-stacked-bar-graph-with-pgfplots?rq=1#=

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

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}

enter image description here

SUNDONG
  • 123
  • 2
    Welcome to TeX.SE! I guess the \makeatletter above \begin{tikzpicture} should be a \begin{document}. –  Dec 30 '18 at 15:34

1 Answers1

2

I cannot tell you why your code does not work. However, your request can be addressed with much more basic tools, too.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\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


\begin{document}
\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={name=leg1,
      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={name=leg2,
      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}
    },
]
\addplot table[x index=0,y index=2] \datatable;
\legend{XGBoost}
\end{axis}
\node[above=2pt of leg1.north] (X) {X\_Group};
\draw[thick] (leg1.west |- X.north) -- (leg1.east |- X.north);
\node[above=2pt of leg2.north] (Y) {Y\_Group};
\draw[thick] (leg2.west |- Y.north) -- (leg2.east |- Y.north);
\end{tikzpicture}
\end{document}

enter image description here