在堆叠图中添加水平线

在堆叠图中添加水平线

我不知道如何在堆叠图中添加几条水平线。如果我不使用堆叠布局,一切都会正常(这是示例)。这个想法是让“总计”列消失,只在一列中显示“B 除以 A”

\begin{tikzpicture} \begin{axis}[
    xtick=data,
    x tick label style={/pgf/number format/1000 sep=,rotate=45,anchor=east},
    enlargelimits=0.1,
    ymin=0.,
    ymax=350.,
    ylabel=hETD,
    legend style={at={(0.5,-0.15)},
        anchor=north,legend columns=-1},
    ybar,
    bar width=12pt,
]
\addplot  coordinates {(2019,176) (2020,121) (2021,118) (2022,141) };
\addplot  coordinates {(2019,148) (2020,176) (2021,204) (2022,122) };
\addplot  coordinates {(2019,324) (2020,298) (2021,322) (2022,263) };
    \addplot [red,line legend,
        sharp plot,update limits=false,
    ] coordinates { (2018,150) (2023,150) }
        node [above] at (2020,150) {a first line};
    \addplot [black,line legend,
        sharp plot,update limits=false,
    ] coordinates { (2018,302) (2023,302) }
        node [above] at (2020,302) {Average};
    \legend{A,B, A+B}
\end{axis}
\end{tikzpicture}

答案1

尝试使用以下命令绘制这些线条\draw

 \documentclass[margin=3mm]{standalone}
 \usepackage{pgfplots}
 \pgfplotsset{compat=1.18}
 
 \begin{document}
    \begin{tikzpicture}[
 lbl/.style = {fill=white, inner sep=2pt, 
               fill opacity=0.5, text opacity=1,
               above}
                        ]
\begin{axis}[
    ybar stacked,
    x=24pt,
    enlarge x limits=0.25,
    axis on top,
%
    tick style = semithick,
    xtick=data,
    x tick label style={/pgf/number format/1000 sep=,
                        rotate=45, anchor=north east,
                        },
    ymin=0,
    ylabel=hETD,
    legend style={at={(0.5,-0.17)},
                  anchor=north, legend columns=-1,
                  /tikz/every even column/.append style={column sep=1em}},
]
\draw[red]  (2018,150) -- node [lbl] {a first line} (2023,150);
\draw       (2018,302) -- node [lbl] {Average} (2023,302);
%
\addplot  coordinates {(2019,176) (2020,121) (2021,118) (2022,141) };
\addplot  coordinates {(2019,148) (2020,176) (2021,204) (2022,122) };
\addplot  coordinates {(2019,324) (2020,298) (2021,322) (2022,263) };
%
    \legend{A , B, A+B}
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容