我正在使用
pgfplots
显示堆积条形图。对于每个 x 值,两个条形堆叠在一起。我需要在每个条形内显示 y 值,并在顶部显示它们的总和。
我设法实现了这一点,但是,第一个条形图太短,无法正确显示内部值。仅对于第一个栏,我只想在顶部显示总和,同时隐藏内部的值。
我不知道如何仅对第一个柱进行这样的例外。
这是代码:
\begin{tikzpicture}
\pgfplotsset{
selective show sum on top/.style={
/pgfplots/scatter/@post marker code/.append code={%
\ifnum\coordindex=#1
\node[
at={(normalized axis cs:%
\pgfkeysvalueof{/data point/x},%
\pgfkeysvalueof{/data point/y})%
},
anchor=south,
]
{\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}};
\fi
},
},selective show sum on top/.default=0
}
\begin{axis}[%
ybar stacked,
ymin=0,
bar width=24pt,
xtick=data,
nodes near coords,
ylabel=Run time (s),
xlabel=$T$,
major x tick style = transparent,
enlarge x limits=0.15,
legend cell align={left},
legend style={
at={(0.02,0.885)},
anchor=west,
column sep=1ex
}
]
\addplot [fill=blue!60] coordinates {
(0,2)
(1,150)
(2,300)
(3,450)
};
\addplot [fill=red!60,selective show sum on top/.list={0,1,2,3}] coordinates {
(0,1)
(1,75)
(2,150)
(3,225)
};
\legend{Computation,Communication}
\end{axis}
\end{tikzpicture}
我尝试在 addplot 中使用“节点附近的坐标*”选项,但它没有按预期工作。