我有如下所示的分组条形图。
我希望每个红色和蓝色条之间的距离稍微大一点。当然,我可以在红色和蓝色条之间添加另一个白色条 - 但这不是很干净的解决方案,而且会弄乱图例(我试过forget plot
在这里使用,但它会让整个条消失)。任何关于如何做到这一点的想法都将不胜感激。
\documentclass{standalone}
\usepackage{pgfplots,pgfplotstable} % plots
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ymax=100,
ymin=0,
ybar,
xtick=data,
legend style={at={(0.39,0.97)},
anchor=north,legend columns=2},
x tick label style={rotate=35,anchor=east},
symbolic x coords={A, B, C},
bar width=9pt,
enlarge x limits=0.2
]
\addplot+[ybar, fill=green] plot coordinates {
(A, 58.83)
(B, 50.50)
(C, 63.67)
};
\addplot+[ybar, fill=red] plot coordinates {
(A, 37.83)
(B, 45.75)
(C, 34.92)
};
\addplot+[ybar, fill=blue] plot coordinates {
(A, 70.17)
(B, 59.50)
(C, 75.17)
};
\addplot+[ybar, fill=yellow] plot coordinates {
(A, 28.50)
(B, 38.67)
(C, 23.75)
};
\legend{\strut 1st, \strut 2nd, \strut 3rd, \strut 4th}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
\documentclass{standalone}
\usepackage{pgfplots,pgfplotstable} % plots
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ymax=100,
ymin=0,
ybar,
xtick=data,
legend style={at={(0.39,0.97)},
anchor=north,legend columns=2},
x tick label style={rotate=35,anchor=east},
symbolic x coords={A, B, C},
bar width=9pt,
enlarge x limits=0.2
]
\addplot+[ybar, fill=green, bar shift=-20 pt] plot coordinates {
(A, 58.83)
(B, 50.50)
(C, 63.67)
};
\addplot+[ybar, fill=red, bar shift=-8 pt] plot coordinates {
(A, 37.83)
(B, 45.75)
(C, 34.92)
};
\addplot+[ybar, fill=blue, bar shift=8 pt] plot coordinates {
(A, 70.17)
(B, 59.50)
(C, 75.17)
};
\addplot+[ybar, fill=yellow, bar shift=20 pt] plot coordinates {
(A, 28.50)
(B, 38.67)
(C, 23.75)
};
\legend{\strut 1st, \strut 2nd, \strut 3rd, \strut 4th}
\end{axis}
\end{tikzpicture}
\end{document}