绘制的图像就是我想要的。我已经有了水平条,我只需要 y 轴标签。此外,我如何将百分比添加到 x 轴值和条形图内的值中?这是我目前所拥有的:
\begin{tikzpicture}
%\draw (0,0) rectangle (6,6); %create a bounding box to reserve space
%\begin{pgflowlevelscope}{\pgftransformscale{.79}}
\begin{axis}[
name=MyAxis,
xbar stacked,
width=10cm, height=10.0cm,
bar width=8pt,
nodes near coords,
nodes near coords style={
font=\scriptsize,
},
xmin=0, xmax=100,
%enlargelimits=0.15,
%enlarge y limits=0.5,
%enlargelimits=0.07,
legend style={at={(0.5,1)}, anchor=south, legend columns=-1, draw=none},
tick label style={font=\small},
%tickwidth = 0pt,
ylabel={Label 1},
symbolic y coords={P11, P12, P61, P62, P81, P82},
ytick=data,
]
\addplot coordinates {(70,P11) (50,P12) (74,P61) (65,P62) (55,P81) (43,P82)};
\addplot coordinates {(70,P11) (50,P12) (74,P61) (65,P62) (55,P81) (43,P82)};
\addplot coordinates {(70,P11) (25,P12) (74,P61) (65,P62) (55,P81) (43,P82)};
\addplot coordinates {(70,P11) (25,P12) (74,P61) (65,P62) (55,P81) (43,P82)};
\addplot[color=violet, fill=violet!50] coordinates {(4,P11) (8,P12) (1,P61) (2,P62) (5,P81) (19,P82)};
\legend{\strut A, \strut D, \strut L, \strut P, \strut T}
\end{axis}
\end{tikzpicture}%
答案1
请注意,由于您的设置,图表中的大部分条形图被剪掉了,但我认为这是故意的。一般来说,你可以使用yticklabel cs
旁边绘制一些东西是勾选标签,例如使用\draw[<->] (yticklabel cs:0) -- (yticklabel cs:0.5) node[midway, left] {Label 1};
。但是,由于您似乎需要剪切图,因此您无法直接在环境内执行此操作,axis
因为它会与图一起被剪切掉。一种解决方法是将 放置\coordinate
在axis
环境内,并使用这些在环境外的标签处绘制事物。
因此,你可以做这样的事情:
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
name=MyAxis,
xbar stacked,
width=10cm, height=10cm,
bar width=8pt,
nodes near coords,
nodes near coords style={
font=\scriptsize,
},
xmin=0, xmax=100,
%enlargelimits=0.15,
%enlarge y limits=0.5,
%enlargelimits=0.07,
legend style={at={(0.5,1)}, anchor=south, legend columns=-1, draw=none},
tick label style={font=\small},
%tickwidth = 0pt,
%ylabel={},
symbolic y coords={P11, P12, P61, P62, P81, P82},
ytick=data,
]
\addplot coordinates {(70,P11) (50,P12) (74,P61) (65,P62) (55,P81) (43,P82)};
\addplot coordinates {(70,P11) (50,P12) (74,P61) (65,P62) (55,P81) (43,P82)};
\addplot coordinates {(70,P11) (25,P12) (74,P61) (65,P62) (55,P81) (43,P82)};
\addplot coordinates {(70,P11) (25,P12) (74,P61) (65,P62) (55,P81) (43,P82)};
\addplot[color=violet, fill=violet!50] coordinates {(4,P11) (8,P12) (1,P61) (2,P62) (5,P81) (19,P82)};
\legend{\strut A, \strut D, \strut L, \strut P, \strut T}
\coordinate (A) at (yticklabel cs:0);
\coordinate (B) at (yticklabel cs:0.5);
\coordinate (C) at (yticklabel cs:1);
\end{axis}
\draw[<->, shorten >=5pt, shorten <=5pt] (A) -- (B) node[midway, left] {Label 1};
\draw[<->, shorten >=5pt, shorten <=5pt] (B) -- (C) node[midway, left] {Label 2};
\end{tikzpicture}%
\end{document}