Can someone help me with putting error bars on multiple bar graphs. This is what I currently have and it doesn't look any good, so far 🙁
\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}
Best Answer
The error bars aren't aligned with the bars because you're using the
ybar interval
plot style. I guess this would count as a missing feature, but in your case, you shouldn't actually be usingybar interval
anyway, but simplyybar
, since you're not trying to plot bars with different data dependent widths.To get a decent looking plot, you can try the following:
ybar=0pt
to remove the gaps within the groups.enlargelimits={abs=0.5}
to set the margin on the outside of the plot to be equal to half the distance between the categories. This requires a recent version of PGFPlots, and requires setting\pgfplotsset{compat=1.7}
or newer in the preamble.bar width=0.15
(or some other value that's smaller than0.2
) to make the bars thin enough to create a gap between the groups. With five bars per group, setting the value to0.2
would result in all the bars sitting flush next to each other.