[Tex/LaTex] How to shift error bars along with data bars

bar chartpgfplotstikz-pgf

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}

Best Answer

The simplest way to position the error bars correctly is to use yshift=<value> to shift the error bars up or down. If you use the same <value> that you used for the bar shift, the error bars will be positioned correctly. A "proper" automatic solution in this case would require support for asymmetric error bars, which isn't currently possible with pgfplots. It might be worth opening a feature request for this.

\documentclass{article}

\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}

\begin{figure}
    \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, black, error bars/.cd,x dir=plus,  x explicit}}
    \pgfplotsset{errm/.style={err, black, 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, yshift=3pt, errm] table[x index=1, y index=0, x error index=2]{\tablei};
    \addplot+[no markers, yshift=3pt, 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, yshift=-3pt, errm] table[x index=1, y index=0, x error index=2]{\tablet};
    \addplot+[no markers, yshift=-3pt, errp] table[x index=1, y index=0, x error index=3]{\tablet};

    \end{axis}
    \end{tikzpicture}

\end{figure}

\end{document}
Related Question