我在 PGFplot 中使用堆叠条形图时遇到问题。我想显示每个段的部分数字,如下所示:
但事实并非如此,当我在 TeXShop 中复制代码并编译它时,我得到了每个段的值的总和:
我使用了完全相同的代码,如以下 URL 所示:
http://pgfplots.net/tikz/examples/stacked-bar-plot/
您知道我需要改变什么才能获得所需的表现吗?
多谢。
答案1
的文档pgfplots
包含以下内容(特别强调最后一段):
因此,正如示例代码开头的注释所暗示的那样,您必须至少将兼容性集更改pgfplots
为:1.9
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
% \pgfplotsset{width=7cm,compat=1.8}
\pgfplotsset{width=7cm,compat=1.9}% Changed here, only
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
bar width=15pt,
nodes near coords,
enlargelimits=0.15,
legend style={at={(0.5,-0.20)},
anchor=north,legend columns=-1},
ylabel={\#participants},
symbolic x coords={tool1, tool2, tool3, tool4,
tool5, tool6, tool7},
xtick=data,
x tick label style={rotate=45,anchor=east},
]
\addplot+[ybar] plot coordinates {(tool1,0) (tool2,2)
(tool3,2) (tool4,3) (tool5,0) (tool6,2) (tool7,0)};
\addplot+[ybar] plot coordinates {(tool1,0) (tool2,0)
(tool3,0) (tool4,3) (tool5,1) (tool6,1) (tool7,0)};
\addplot+[ybar] plot coordinates {(tool1,6) (tool2,6)
(tool3,8) (tool4,2) (tool5,6) (tool6,5) (tool7,6)};
\addplot+[ybar] plot coordinates {(tool1,4) (tool2,2)
(tool3,0) (tool4,2) (tool5,3) (tool6,2) (tool7,4)};
\legend{\strut never, \strut rarely, \strut sometimes, \strut often}
\end{axis}
\end{tikzpicture}
\end{document}
瞧瞧: