有人能帮我在多个条形图上添加误差线吗?这是我目前所拥有的,到目前为止看起来并不好 :(
\begin{figure}
\begin{tikzpicture}
\begin{axis}[
x tick label style={
/pgf/number format/1000 sep=},
ylabel=Accuracy,
enlargelimits=0.05,
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=-1},
ybar interval=0.7,
]
\addplot+[error bars/.cd,
y dir=both,y explicit]
coordinates {
(1,80) +- (0.0, 0.5)
(2,81) +- (0.0, 1.5)
(3,90) +- (0.0, 1.5)
(4,92) +- (0.0, 0.6)
(5,93) +- (0.0, 0.9)};
\addplot+[error bars/.cd,
y dir=both,y explicit]
coordinates {
(1,90) +- (0.0, 0.5)
(2,86) +- (0.0, 1.5)
(3,80) +- (0.0, 1.5)
(4,82) +- (0.0, 0.6)
(5,83) +- (0.0, 0.9)};
\addplot+[error bars/.cd,
y dir=both,y explicit]
coordinates {
(1,80) +- (0.0, 0.5)
(2,81) +- (0.0, 1.5)
(3,90) +- (0.0, 1.5)
(4,92) +- (0.0, 0.6)
(5,93) +- (0.0, 0.9)};
\addplot+[error bars/.cd,
y dir=both,y explicit]
coordinates {
(1,90) +- (0.0, 0.5)
(2,86) +- (0.0, 1.5)
(3,80) +- (0.0, 1.5)
(4,82) +- (0.0, 0.6)
(5,83) +- (0.0, 0.9)};
\addplot+[error bars/.cd,
y dir=both,y explicit]
coordinates {
(1,90) +- (0.0, 0.5)
(2,86) +- (0.0, 1.5)
(3,80) +- (0.0, 1.5)
(4,82) +- (0.0, 0.6)
(5,83) +- (0.0, 0.9)};
\legend{A,B,C,D,E}
%\draw ({rel axis cs:0,0}|-{axis cs:0,0}) -- ({rel axis cs:1,0}|-{axis cs:0,0});
\end{axis}
\end{tikzpicture}
\end{figure}
答案1
由于您使用的是绘图样式,因此误差线与条形图不对齐ybar interval
。我猜这应该算作一个缺失的功能,但就您而言,您实际上不应该使用ybar interval
,而应该简单地使用ybar
,因为您不是试图绘制具有不同数据相关宽度的条形图。
为了获得不错的效果,您可以尝试以下操作:
- 设置
ybar=0pt
以消除组内的间隙。 - 设置
enlargelimits={abs=0.5}
为将图外部的边距设置为等于类别之间距离的一半。这需要较新版本的 PGFPlots,并且需要\pgfplotsset{compat=1.7}
在序言中设置或更新版本。 - 设置
bar width=0.15
(或其他小于的值0.2
)以使条形足够细,从而在组之间产生间隙。如果每组有 5 个条形,则将值设置为0.2
将导致所有条形彼此齐平。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend pos=outer north east,
enlargelimits={abs=0.5},
ybar=0pt,
bar width=0.15,
xtick={0.5,1.5,...,5.5},
xticklabels={1,...,5},
x tick label as interval
]
\addplot+[error bars/.cd,
y dir=both,y explicit]
coordinates {
(1,80) +- (0.0, 0.5)
(2,81) +- (0.0, 1.5)
(3,90) +- (0.0, 1.5)
(4,92) +- (0.0, 0.6)
(5,93) +- (0.0, 0.9)};
\addplot+[error bars/.cd,
y dir=both,y explicit]
coordinates {
(1,90) +- (0.0, 0.5)
(2,86) +- (0.0, 1.5)
(3,80) +- (0.0, 1.5)
(4,82) +- (0.0, 0.6)
(5,83) +- (0.0, 0.9)};
\addplot+[error bars/.cd,
y dir=both,y explicit]
coordinates {
(1,80) +- (0.0, 0.5)
(2,81) +- (0.0, 1.5)
(3,90) +- (0.0, 1.5)
(4,92) +- (0.0, 0.6)
(5,93) +- (0.0, 0.9)};
\addplot+[error bars/.cd,
y dir=both,y explicit]
coordinates {
(1,90) +- (0.0, 0.5)
(2,86) +- (0.0, 1.5)
(3,80) +- (0.0, 1.5)
(4,82) +- (0.0, 0.6)
(5,83) +- (0.0, 0.9)};
\addplot+[error bars/.cd,
y dir=both,y explicit]
coordinates {
(1,90) +- (0.0, 0.5)
(2,86) +- (0.0, 1.5)
(3,80) +- (0.0, 1.5)
(4,82) +- (0.0, 0.6)
(5,83) +- (0.0, 0.9)};
\legend{A,B,C,D,E}
%\draw ({rel axis cs:0,0}|-{axis cs:0,0}) -- ({rel axis cs:1,0}|-{axis cs:0,0});
\end{axis}
\end{tikzpicture}
\end{document}