为什么这个 pgfplot 示例中没有出现 X_Group、Y_Group?我尝试稍微更改下面的参考,但我的示例没有显示示例中的分组。
\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}
下面是barplot,是当前编译的结果,并且我添加了想要的结果(要添加X_Group,Y_Group)。
我尝试更改draw group line={[index]2}
为,draw group line={[index]3}
因为分组信息位于表格的第三列,但它仅显示分组,而缺少前一个条形图,如下图所示:(
draw group line={[index]3}{1}{X\_Group}{-3.5ex}{7pt},
draw group line={[index]3}{2}{Y\_Group}{-3.5ex}{7pt}
答案1
我无法告诉你为什么你的代码不起作用。不过,你的请求也可以通过更基本的工具来解决。
\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}