7

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}
  • Welcome to tex.stackexchange! Could you please provide us a complete Minimal Working Example, so that we can understand in what environment you're working on? Thank you in advance. – enigmator Sep 2 '14 at 16:54
10

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}
  • Excellent. Thank you very much. Now, on the x-axis, instead of the numbers pointing to the middle bar, can I put lines to separate the groups and then remove the tick pointer above the numbers. – tx008l Sep 2 '14 at 19:55
  • Sorry, talked too soon. The \pgfplotsset{compat=1.10} is causing an error on my MikTex 2.9 compilation. Let me play around with it for a moment. – tx008l Sep 2 '14 at 20:08
  • @tx008l: Actually, \pgfplotsset{compat=1.7} or newer works as well. If your version of PGFPlots is older than that, you should definitely update it. I also edited my answer to show a way to get the interval labels, but it's a bit manual. – Jake Sep 2 '14 at 20:34
  • Thanx; I needed to upgrade. Worked perfectly with \pgfplotsset{compat=1.8}. Thanx again for the excellent prompt response. – tx008l Sep 3 '14 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.