如何在堆叠条形图上添加一些误差线?

如何在堆叠条形图上添加一些误差线?

我正在尝试重现以下图表: 图表重现

我已经设法得到了它,但我不知道如何放置错误栏: 在此处输入图片描述

有人知道我该怎么做吗?

这是我的当前代码。

\documentclass[11pt]{report}
\usepackage{tikz,pgfplots,pgfplotstable}
\usepackage{lipsum}

\usepackage{xcolor}
\definecolor{bluereport}{RGB}{79, 129, 189}

\pgfplotsset{width=10cm, compat=1.9}

\begin{document}

\pgfplotstableread{
Label   A   B   C    D    err.A  err.B   err.C   err.D
OP1     17  51  97   85   1.65   19.52   57.11   32.48
OP2     18  84  129  54   1.65   19.52   57.11   32.48
OP3     20  49  208  119  1.65   19.52   57.11   32.48
    }\data

\begin{tikzpicture}
\begin{axis}[
    title style={align=center}, 
    title = {my title},
    ybar stacked,
    bar width=30pt,
    ytick={0, 60, 120, 180, 240, 300, 360, 420, 480},
    ymax=480,
    xtick=data,
    xticklabels from table={\data}{Label},
    ylabel={Time (min)},
    enlargelimits=0.15,
    tickwidth= 5pt,
    ymajorgrids=true,
    grid style=dashed,
    legend style={at={(0.5,-0.15)},
    legend style={/tikz/every even column/.append style={column sep=0.5cm}},
    anchor=north,
    legend columns=-1},
] 
\addplot [bluereport!90!black,fill=bluereport!100!white]    table[y=A, meta=Label, x expr=\coordindex] {\data};
\addlegendentry{A}

\addplot [bluereport!95!white,fill=bluereport!75!white] table[y=B, meta=Label, x expr=\coordindex] {\data};
\addlegendentry{B}

\addplot [bluereport!70!white,fill=bluereport!50!white] table[y=C, meta=Label, x expr=\coordindex] {\data};
\addlegendentry{C}

\addplot [bluereport!45!white,fill=bluereport!25!white] table[y=D, meta=Label, x expr=\coordindex] {\data};
\addlegendentry{D}

\end{axis}  
\end{tikzpicture}

\end{document}

感谢您的帮助。

答案1

欢迎!需要“拆分”图,以便可以使用此解决方案,以便在正确的垂直位置添加误差线。

\documentclass[11pt]{report}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\definecolor{bluereport}{RGB}{79, 129, 189}

\pgfplotsset{width=10cm, compat=1.17}

\makeatletter% from https://tex.stackexchange.com/a/13665
\newcommand\resetstackedplots{%
\pgfplots@stacked@isfirstplottrue
}
\makeatother
\begin{document}

\pgfplotstableread{
Label   A   B   C    D    err.A  err.B   err.C   err.D
OP1     17  51  97   85   1.65   19.52   57.11   32.48
OP2     18  84  129  54   1.65   19.52   57.11   32.48
OP3     20  49  208  119  1.65   19.52   57.11   32.48
    }\data

\begin{tikzpicture}
\begin{axis}[
    title style={align=center}, 
    title = {my title},
    bar width=30pt,
    ytick={0, 60, 120, 180, 240, 300, 360, 420, 480},
    ymax=480,
    xtick=data,
    xticklabels from table={\data}{Label},
    ylabel={Time (min)},
    enlargelimits=0.15,
    tickwidth= 5pt,
    ymajorgrids=true,
    grid style=dashed,
    legend style={at={(0.5,-0.15)},
    legend style={/tikz/every even column/.append style={column sep=0.5cm}},
    anchor=north,
    legend columns=-1},
] 

\addplot [ybar stacked,bluereport!90!black,fill=bluereport!100!white]    table[y=A, meta=Label, x expr=\coordindex] {\data};
\addlegendentry{A}

\addplot [ybar stacked,bluereport!95!white,fill=bluereport!75!white] table[y=B, meta=Label, x expr=\coordindex] {\data};
\addlegendentry{B}

\addplot [ybar stacked,bluereport!70!white,fill=bluereport!50!white] table[y=C, meta=Label, x expr=\coordindex] {\data};
\addlegendentry{C}

\addplot [ybar stacked,bluereport!45!white,fill=bluereport!25!white] table[y=D, meta=Label, x expr=\coordindex] {\data};
\addlegendentry{D}

\resetstackedplots
\pgfplotsinvokeforeach{A,B,C,D}{
\addplot[forget plot,draw=none,stack plots=y,
     error bars/.cd, y dir=both, y explicit] 
    table[y=#1,  x expr=\coordindex,y error expr=\thisrow{err.#1}]
 {\data};}

\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容