我有一个改编自此的代码关联。我的问题是在分组条形图之间添加线条和空白。此代码生成了条形图,但在此条形图中,分组条形彼此非常接近。我需要在每个分组条形之间添加空间和线条。你能帮我做吗?谢谢。
\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 53.0 36.027 8.219 51.0 99.726 1
2 56.0 21.918 4.110 53.0 12.329 1
3 56.0 16.438 6.849 55.0 39.726 1
4 64.0 2.740 2.740 56.0 28.767 1
5 55.0 1.370 6.849 52.0 1.3700 2
6 58.0 2.740 16.438 52.0 57.534 2
7 58.0 0.000 0.000 55.0 32.877 2
8 63.0 6.849 5.479 56.0 15.068 2
9 56.0 12.329 6.849 50.0 20.548 3
10 54.0 4.110 8.219 52.0 35.616 3
11 58.0 12.329 6.849 54.0 20.548 3
12 64.0 4.110 8.219 57.0 35.616 3
}\datatable
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ylabel=Accuracy (\%),
xtick=data,
xticklabels=\empty,
enlarge y limits=false,
enlarge x limits=0.05,
ymin=40, ymax=65,
ybar=2.0cm,
bar width=9pt,
legend style={
font=\footnotesize,
cells={anchor=west},
legend columns=7,
at={(0.3,-0.25)},
anchor=north,
/tikz/every even column/.append style={column sep=0.5cm},
},
]
\addplot table[x index=0,y index=1] \datatable;
\legend{test1}
\end{axis}
\begin{axis}[
ylabel=Accuracy (\%),
xtick=data,
xticklabels={50,100,200,400,50,100,200,400,50,100,200,400},
enlarge y limits=false,
enlarge x limits=0.05,
ymin=40,ymax=65,
legend style={
font=\footnotesize,
cells={anchor=west},
legend columns=7,
at={(0.71,-0.25)},
anchor=north,
/tikz/every even column/.append style={column sep=0.2cm}
},
x tick label style={rotate=90,anchor=east},
draw group line={[index]6}{1}{$\varepsilon$=1.0}{55.0ex}{7pt},
draw group line={[index]6}{2}{$\varepsilon$=2.0}{55.0ex}{7pt},
draw group line={[index]6}{3}{$\varepsilon$=4.0}{55.0ex}{7pt}
]
\addplot+[forget plot] table[x index=0,y index=4, restrict x to domain=0:4] \datatable;
\addplot+[forget plot] table[x index=0,y index=4, restrict x to domain=5:8] \datatable;
\addplot+ table[x index=0,y index=4, restrict x to domain=9:12] \datatable;
\legend{test2}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
通过对条形图的 x 坐标、线条和用于在底部绘制水平线的坐标进行适当的计算,您可以得到组之间的空坐标。垂直线按照样式绘制draw group line
。
\documentclass{article}
\usepackage{pgfplotstable} % loads pgfplots which loads tikz
\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
\pgfplotstablegetelem{\pgfplotstablerow}{[index]0}\of\datatable
% calculate x-coordinate similar to bar chart below
\pgfmathsetmacro{\tmpX}{mod(\pgfplotsretval-1,4) + (#2-1)*5 +1}
\ifnum\thegroupcount=0
\stepcounter{groupcount}
\coordinate [yshift=#4] (startgroup) at (axis cs:\tmpX,0);
\else
\coordinate [yshift=#4] (endgroup) at (axis cs:\tmpX,0);
\fi
\else
\ifnum\thegroupcount=1
\setcounter{groupcount}{0}
\draw [
shorten >=-#5,
shorten <=-#5
] (startgroup) -- node [anchor=north] {#3} (endgroup);
% draws the red lines
\draw [thick,red] ({axis cs:\tmpX+1,0} |- {rel axis cs:0,-0.2}) --
({axis cs:\tmpX+1,0} |- {rel axis cs:0,1.05});
\fi
\fi
}
\ifnum\thegroupcount=1
\setcounter{groupcount}{0}
\draw [
shorten >=-#5,
shorten <=-#5
] (startgroup) -- node [anchor=north] {#3} (endgroup);
\fi
}
}
}
\makeatother
\pgfplotstableread{
1 53.0 36.027 8.219 51.0 99.726 1
2 56.0 21.918 4.110 53.0 12.329 1
3 56.0 16.438 6.849 55.0 39.726 1
4 64.0 2.740 2.740 56.0 28.767 1
5 55.0 1.370 6.849 52.0 1.3700 2
6 58.0 2.740 16.438 52.0 57.534 2
7 58.0 0.000 0.000 55.0 32.877 2
8 63.0 6.849 5.479 56.0 15.068 2
9 56.0 12.329 6.849 50.0 20.548 3
10 54.0 4.110 8.219 52.0 35.616 3
11 58.0 12.329 6.849 54.0 20.548 3
12 64.0 4.110 8.219 57.0 35.616 3
}\datatable
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ylabel=Accuracy (\%),
xtick distance=1,
xtick=\empty,
enlarge y limits=false,
enlarge x limits=0.05,
ymin=40, ymax=65,
ybar=2.0cm,
bar width=9pt,
legend style={
font=\footnotesize,
cells={anchor=west},
legend columns=7,
at={(0.3,-0.25)},
anchor=north,
/tikz/every even column/.append style={column sep=0.5cm},
},
]
% changed x-coordinates
\addplot table[x expr={mod(\coordindex,4) + (\thisrowno{6}-1)*5 +1},y index=1] \datatable;
\legend{test1}
\end{axis}
\begin{axis}[
ylabel=Accuracy (\%),
xtick={1,...,14}, % specify tick positions
xticklabels={50,100,200,400,,50,100,200,400,,50,100,200,400}, % added empty ticklabels
enlarge y limits=false,
enlarge x limits=0.05,
ymin=40,ymax=65,
legend style={
font=\footnotesize,
cells={anchor=west},
legend columns=7,
at={(0.71,-0.25)},
anchor=north,
/tikz/every even column/.append style={column sep=0.2cm}
},
x tick label style={rotate=90,anchor=east},
draw group line={[index]6}{1}{$\varepsilon$=1.0}{55.0ex}{7pt},
draw group line={[index]6}{2}{$\varepsilon$=2.0}{55.0ex}{7pt},
draw group line={[index]6}{3}{$\varepsilon$=4.0}{55.0ex}{7pt}
]
% change x-coordinates, and therefore also the "restrict x to domain" domains
\addplot+[forget plot] table[x index=0,y index=4, restrict x to domain=1:4] \datatable;
\addplot+[forget plot] table[x expr={\thisrowno{0}+2},y index=4, restrict x to domain=6:9] \datatable;
\addplot+ table[x expr={\thisrowno{0}+3},y index=4, restrict x to domain=11:14] \datatable;
\legend{test2}
\end{axis}
\end{tikzpicture}
\end{document}