![来自数据表的堆积条形图,点元数据的格式](https://linux22.com/image/454299/%E6%9D%A5%E8%87%AA%E6%95%B0%E6%8D%AE%E8%A1%A8%E7%9A%84%E5%A0%86%E7%A7%AF%E6%9D%A1%E5%BD%A2%E5%9B%BE%EF%BC%8C%E7%82%B9%E5%85%83%E6%95%B0%E6%8D%AE%E7%9A%84%E6%A0%BC%E5%BC%8F.png)
我有一个数据表,每个 x 有多个 y 值,我已将其排列为堆叠条形图。似乎 pgfplots 可以根据绝对值计算百分比,但我暂时还是自己计算百分比。我已经从这里关于point meta=explicit
,但我无法摆脱“0”值,并且我还希望 x 值显示在其栏的中心。
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread{
Label A B C Aabs Babs Cabs
data1 0.70 0.00 0.30 7 0 3
data2 0.33 0.67 0.00 6 12 0
data3 0.75 0.00 0.25 3 0 1
data4 0.50 0.25 0.25 4 2 2
}\dt
\begin{axis}[
hide x axis,
nodes near coords,
xbar stacked,
xmin=0,
y dir=reverse,
ytick=data,
yticklabels from table={\dt}{Label},
]
\addplot [
fill=red!25,
point meta=explicit,
] table [
x=A,
y expr=\coordindex,
meta=Aabs,
] {\dt};
\addplot [
fill=green!25,
point meta=explicit,
] table [
x=B,
y expr=\coordindex,
meta=Babs,
] {\dt};
\addplot [
fill=blue!25,
point meta=explicit,
] table [
x=C,
y expr=\coordindex,
meta=Cabs,
] {\dt};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
您想要的是堆叠图的默认行为。您需要在选项xbar stacked
之前指定选项,nodes near coords
如下所示:
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepackage{pgfplotstable}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread{
Label A B C Aabs Babs Cabs
data1 0.70 0.00 0.30 7 0 3
data2 0.33 0.67 0.00 6 12 0
data3 0.75 0.00 0.25 3 0 1
data4 0.50 0.25 0.25 4 2 2
}\dt
\begin{axis}[
xbar stacked, nodes near coords,
hide x axis,
xmin=0,
y dir=reverse,
ytick=data,
yticklabels from table={\dt}{Label},
point meta=explicit,
]
\addplot[fill=red!25] table[x=A, y expr=\coordindex, meta=Aabs] {\dt};
\addplot[fill=green!25] table[x=B, y expr=\coordindex, meta=Babs] {\dt};
\addplot[fill=blue!25] table[x=C, y expr=\coordindex, meta=Cabs] {\dt};
\end{axis}
\end{tikzpicture}
\end{document}