[Tex/LaTex] Bar plot with error bars and two data rows

bar chartpgfplotsplot

I am already close to the solution I'm searching for. I want this graph https://tex.stackexchange.com/a/35749/38544 with two data rows.

Thus I took the code, duplicated the addplot-section and removed the labels:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}


\begin{tikzpicture}
\begin{axis}[
    title = {Optimization based upon co-occurences},
    xbar,
    width=10cm,
    xtick={1,...,8},
    xticklabels={%
        atpB,
        atpE,
        atpF,
        atpH,
        atpA,
        atpG,
        atpD,
        atpC},
    grid=major,
    ]

\addplot[
    fill=blue!25,
    draw=black,
    ybar,
    point meta=y,
    every node near coord/.style={inner ysep=5pt},
    error bars/.cd,
        y dir=both,
        y explicit
] 
table [y error=error] {
x   y           error    label
1   -0.279535   0.015982 2 
2   0.739360    0.031211 4
3   -0.279302   0.017384 1 
4   -0.602794   0.022327 1 
5   0.487714    0.015970 8 
6   -0.294501   0.014923 4 
7   0.526527    0.016725 5 
8   -0.297469   0.021122 1
\addplot[
    fill=red!25,
    draw=black,
    ybar,
    point meta=y,
    every node near coord/.style={inner ysep=5pt},
    error bars/.cd,
        y dir=both,
        y explicit
] 
table [y error=error] {
x   y           error    label
1   -0.279535   0.015982 2 
2   0.739360    0.031211 4
3   -0.279302   0.017384 1 
4   -0.602794   0.022327 1 
5   0.487714    0.015970 8 
6   -0.294501   0.014923 4 
7   0.526527    0.016725 5 
8   -0.297469   0.021122 1
};

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

The bar plot already looks nice, but the error Bars get "stuck" in the middle both bars.

Plot

Best Answer

Put the ybar in the axis options instead of the \addplot options. That way, the shift will be applied correctly:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}


\begin{tikzpicture}
\begin{axis}[
    title = {Optimization based upon co-occurences},
    width=10cm,
    xtick={1,...,8},
    xticklabels={%
        atpB,
        atpE,
        atpF,
        atpH,
        atpA,
        atpG,
        atpD,
        atpC},
    grid=major,
    ybar
    ]

\addplot[
    fill=blue!25,
    draw=black,
    point meta=y,
    every node near coord/.style={inner ysep=5pt},
    error bars/.cd,
        y dir=both,
        y explicit
] 
table [y error=error] {
x   y           error    label
1   -0.279535   0.015982 2 
2   0.739360    0.031211 4
3   -0.279302   0.017384 1 
4   -0.602794   0.022327 1 
5   0.487714    0.015970 8 
6   -0.294501   0.014923 4 
7   0.526527    0.016725 5 
8   -0.297469   0.021122 1
};

\addplot[
    fill=red!25,
    draw=black,
    point meta=y,
    every node near coord/.style={inner ysep=5pt},
    error bars/.cd,
        y dir=both,
        y explicit
] 
table [y error=error] {
x   y           error    label
1   -0.279535   0.015982 2 
2   0.739360    0.031211 4
3   -0.279302   0.017384 1 
4   -0.602794   0.022327 1 
5   0.487714    0.015970 8 
6   -0.294501   0.014923 4 
7   0.526527    0.016725 5 
8   -0.297469   0.021122 1
};

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