[Tex/LaTex] pgfplots: Bar graph with confidence intervals (error)

pgfplots

I would like to put a confidence interval (error bars) in my bar graph, but I tried the += and -= and it did not work.

Here is the code:

    \begin{tikzpicture}
    \begin{axis}[
    width  = 0.50*\textwidth,
    height = 8cm,
    major x tick style = transparent,
    ybar=2*\pgflinewidth,
    bar width=25pt,
    ymajorgrids = true,
    symbolic x coords={A,B},
    xtick = data,
    scaled y ticks = false,
    enlarge x limits=0.50,
    ymin=0,
    legend cell align=left,
    legend style={at={(0.5,-0.12)},anchor=north}
]
    \addplot[style={fill=white}]
        coordinates {
        (A, 2.98)
        (B,4.23)};

    \addplot[style={fill=black}]
         coordinates {
         (A,4.33)
         (B,3.82)};

    \legend{Red Graph, Blue Graph}
\end{axis}
\end{tikzpicture}

Best Answer

You have to enable the error bars using something like

error bars/.cd, y dir=both, y explicit

as plot option.

Example:

  \documentclass{standalone}
  \usepackage{pgfplots}
  \pgfplotsset{compat=1.12}
  \begin{document}

      \begin{tikzpicture}
      \begin{axis}[
      width  = 0.50*\textwidth,
      height = 8cm,
      major x tick style = transparent,
      ybar=2*\pgflinewidth,
      bar width=25pt,
      ymajorgrids = true,
      symbolic x coords={A,B},
      xtick = data,
      scaled y ticks = false,
      enlarge x limits=0.50,
      ymin=0,
      legend cell align=left,
      legend style={at={(0.5,-0.12)},anchor=north},
  ]
      \addplot[style={fill=white},error bars/.cd, y dir=both, y explicit]
          coordinates {
          (A, 2.98) += (0,0.2) -= (0,0.1)
          (B,4.23) += (0,0.2) -= (0,0.1)};

      \addplot[style={fill=black},error bars/.cd, y dir=both, y explicit,error bar style=red]
           coordinates {
           (A,4.33) += (0,0.2) -= (0,0.1)
           (B,3.82) += (0,0.5) -= (0,0.2)};

      \legend{White Graph, Black Graph}
  \end{axis}
  \end{tikzpicture}
  \end{document}

enter image description here