Plotbalken werden beim Malen zwischen zwei nicht zusammenhängenden Linien nach rechts verschoben

Plotbalken werden beim Malen zwischen zwei nicht zusammenhängenden Linien nach rechts verschoben

Ich habe ein Balkendiagramm mit einigen horizontalen Linien. Die Linien stellen die Grundlinie und die Standardabweichung dar.

Wenn ich die Zeile kommentiere fill between, werden die Balken in jeder Einheit zentriert. Andernfalls sind sie rechtsbündig. Mache ich etwas falsch? Wie behebe ich das?

\begin{tikzpicture}
    \begin{axis}[
        xlabel={Agents},
        width=0.28\textwidth,
        height=0.25\textwidth,
        enlarge x limits={abs=0.5},
        ybar=0pt,
        bar width=0.25,
        xtick={1.5,2.5,...,9.5},
        xticklabels={2,...,9},
        x tick label as interval,
        xmin=5, xmax=9,
        ymax=140
    ]

    \addplot[draw=black,smooth,dashed] coordinates {(4.5,111)(9.5,111)};
    \addplot[name path=black_top,smooth,color=black!50] coordinates {(4.5,136)(9.5,136)};
    \addplot[name path=black_down,smooth,color=black!50] coordinates {(4.5,86)(9.5,86)};
    \addplot[black!20,fill opacity=0.2] fill between [of=black_top and black_down]; % THIS LINE BREAKS THE BARS

    \addplot+[color=black,fill=green,error bars/.cd,y dir=both,y explicit] coordinates {( 5 ,  94.87666666666667 )+-(0, 23.40752294959781 )( 6 ,  80.05466666666666 )+-(0, 21.522437276457225 )( 7 ,  64.15666666666667 )+-(0, 16.67690645011687 )( 8 ,  60.133 )+-(0, 18.43625402583517 )( 9 ,  50.91766666666666 )+-(0, 16.16000343323637 ) };
    \addplot+[color=black,fill=yellow,error bars/.cd,y dir=both,y explicit] coordinates {( 5 ,  95.51833333333333 )+-(0, 23.22793158057708 )( 6 ,  84.58733333333333 )+-(0, 23.009232926050753 )( 7 ,  75.2744488977956 )+-(0, 26.206436774916398 )( 8 ,  58.797 )+-(0, 15.941772440729855 )( 9 ,  53.17666666666667 )+-(0, 14.808804487817241 ) };
    \addplot+[color=black,fill=red,error bars/.cd,y dir=both,y explicit] coordinates {( 5 ,  94.84100000000001 )+-(0, 22.82644169743311 )( 6 ,  78.558 )+-(0, 18.713503938615418 )( 7 ,  68.35833333333333 )+-(0, 16.593949039798076 )( 8 ,  63.76233333333332 )+-(0, 16.30226192649106 )( 9 ,  57.321333333333335 )+-(0, 15.308328400611293 ) };
    \end{axis}
\end{tikzpicture}

Antwort1

Ich bin nicht sicher, ob das ein Fehler oder eine Funktion ist, aber Sie können das gewünschte Diagramm auch ohne die fillbetweenBibliothek zeichnen. Siehe den folgenden Code.

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        compat=1.11,
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xlabel={Agents},
        enlarge x limits={abs=0.5},
        ybar=0pt,
        bar width=0.25,
        xtick={1.5,2.5,...,9.5},
        xticklabels={2,...,9},
        x tick label as interval,
        xmin=5, xmax=9,
        ymax=140,
        error bars/y dir=both,
        error bars/y explicit,
    ]

        \path [draw=black!50,fill=black!20,fill opacity=0.2]
            (\pgfkeysvalueof{/pgfplots/xmin},86)
                rectangle
                    (\pgfkeysvalueof{/pgfplots/xmax},136);
        \addplot [draw=black,smooth,dashed] coordinates {(4.5,111)(9.5,111)};

        \addplot [fill=green] coordinates {
            ( 5 ,  94.87666666666667  )+-(0, 23.40752294959781 )
            ( 6 ,  80.05466666666666  )+-(0, 21.522437276457225 )
            ( 7 ,  64.15666666666667  )+-(0, 16.67690645011687 )
            ( 8 ,  60.133             )+-(0, 18.43625402583517 )
            ( 9 ,  50.91766666666666  )+-(0, 16.16000343323637 )
        };
        \addplot [fill=yellow] coordinates {
            ( 5 ,  95.51833333333333  )+-(0, 23.22793158057708 )
            ( 6 ,  84.58733333333333  )+-(0, 23.009232926050753 )
            ( 7 ,  75.2744488977956   )+-(0, 26.206436774916398 )
            ( 8 ,  58.797             )+-(0, 15.941772440729855 )
            ( 9 ,  53.17666666666667  )+-(0, 14.808804487817241 )
        };
        \addplot [fill=red] coordinates {
            ( 5 ,  94.84100000000001  )+-(0, 22.82644169743311 )
            ( 6 ,  78.558             )+-(0, 18.713503938615418 )
            ( 7 ,  68.35833333333333  )+-(0, 16.593949039798076 )
            ( 8 ,  63.76233333333332  )+-(0, 16.30226192649106 )
            ( 9 ,  57.321333333333335 )+-(0, 15.308328400611293 )
        };

    \end{axis}
\end{tikzpicture}
\end{document}

Bild, das das Ergebnis des obigen Codes zeigt

verwandte Informationen