[Tex/LaTex] How to draw Latex multiple bar chart with error bar

bar charterrorstikz-pgf

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 using ybar interval anyway, but simply ybar, since you're not trying to plot bars with different data dependent widths.

To get a decent looking plot, you can try the following:

  • Set ybar=0pt to remove the gaps within the groups.
  • Set 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.
  • Set bar width=0.15 (or some other value that's smaller than 0.2) to make the bars thin enough to create a gap between the groups. With five bars per group, setting the value to 0.2 would result in all the bars sitting flush next to each other.

\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}