I am trying to create a figure with two bar graphs with bars grouped two by two.
I use bar width and bar shift to separate the bars.
My problem is that I want to add error bars to these plots but I do
not know how to make them follow the bars.
All I get are couples of bars (good!) but with couples of error bars
between them (awkward!).
How could I have the error bars in the middle of the data bars without manually shifting them?
My current attempt:
\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread{
1 4 0.2 0.1
2 4.2 0.1 0.5
3 3.1 0.3 0.4
4 2.5 0.25 0.35
}\tablei
\pgfplotstableread{
1 3.5 0.1 0.3
2 3.8 0.2 0.2
3 4.0 0.25 0.25
4 3.0 0.3 0.5
}\tablet
% Bar styles
\pgfplotsset{shifti/.style={mark=no markers, bar width=4pt, bar shift=3pt}}
\pgfplotsset{shiftt/.style={mark=no markers, bar width=4pt, bar shift=-3pt}}
% Error styles
\pgfplotsset{err/.style={forget plot, draw=none}}
\pgfplotsset{errp/.style={err, error bars/.cd,x dir=plus, x explicit}}
\pgfplotsset{errm/.style={err, error bars/.cd,x dir=minus, x explicit}}
\begin{axis}[%
/pgfplots/table/header=false,
scale only axis,
width=12cm,
height=8cm,
axis on top]
\addplot+[xbar, shifti] table[x index=1, y index=0]{\tablei};
\addplot+[no markers, errm] table[x index=1, y index=0, x error index=2]{\tablei};
\addplot+[no markers, errp] table[x index=1, y index=0, x error index=3]{\tablei};
\addplot+[xbar, shiftt] table[x index=1, y index=0]{\tablet};
\addplot+[no markers, errm] table[x index=1, y index=0, x error index=2]{\tablet};
\addplot+[no markers, errp] table[x index=1, y index=0, x error index=3]{\tablet};
\end{axis}
\end{tikzpicture}
\end{document}

texfiles and two datafiles. You can include the data in your document using the\begin{filecontents}{<filename>}...\end{filecontents}environment. 2) You seem to be happy to shift the bars manually (instead of just supplyingxbarto the axis, which would take care of the offset automatically). Why don't you want to shift the error bars in the same way? – Jake May 7 '12 at 8:20xbarin the axis declaration does not give the wanted result because I fail to prevent the (data) bars associated with the plots for the error bars to be drawn. I tried to shift manually the error bars but with no success. – Alfred M. May 7 '12 at 17:19