\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\pgfplotsset{testbar2/.style={
xbar stacked,
width=0.8\textwidth,
xmajorgrids = true,
xmin=0,xmax=100,
ytick = data, yticklabels = {1,2},
tick align = outside, xtick pos = left,
bar width=6mm, y=13mm,
nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%}, % <-- prints % sign after y coordinate value
xticklabel={\pgfmathprintnumber{\tick}\%},% <-- prints % sign after x tick value
nodes near coords align={center}, % <-- horizontal alignment centered of nodes
enlarge y limits=0.75, % <-- Adds vertical space so to not crop the bars
}}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[testbar2, legend style={at={(0.5,-0.3)}, anchor=north,legend columns=-1},
/tikz/every even column/.append style={column sep=0.2cm}, every node near coord/.append style={yshift=0.5cm},
]
\addplot coordinates{(20.00,1) (22.60,2)};
\addplot coordinates{(28.57,1) (23.10,2)};
\addplot coordinates{(11.43,1) (12.14,2)};
\addplot coordinates{(28.57,1) (23.70,2)};
\addplot coordinates{(6.43,1) (8.07,2)};
\addplot coordinates{(5.00,1) (10.39,2)};
\legend{A,B,A\textsubscript{1},B\textsubscript{1},A\textsubscript{2},C}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
答案1
这可以是一种替代方案。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\pgfplotsset{,
xmin=0,
/tikz/font=\footnotesize,
}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
height=85pt,
width=\axisdefaultwidth,
xmajorgrids = true,
xmin=0,xmax=100,
ytick = data, yticklabels = {1,2},
xtick={0,20,...,100},
tick align = outside, xtick pos = left,
x post scale=1.3,
scale only axis,
enlarge y limits=0.75,
xbar stacked,
bar width=6mm, y=14mm,
ytick=data,
nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%},
nodes near coords style={yshift=(-1)^\plotnum * 13pt},% shifts up or down nodes near coords <<<
xticklabel={\pgfmathprintnumber{\tick}\%},% <-- prints % sign after x tick value
legend style={at={(0.5,-0.3)}, anchor=north,legend columns=-1},
]
\addplot coordinates{(20.00,1) (22.60,2)};
\addplot coordinates{(28.57,1) (23.10,2)};
\addplot coordinates{(11.43,1) (12.14,2)};
\addplot coordinates{(28.57,1) (23.70,2)};
\addplot coordinates{(6.43,1) (8.07,2)};
\addplot coordinates{(5.00,1) (10.39,2)};
\legend{A,B,A\textsubscript{1},B\textsubscript{1},A\textsubscript{2},C}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
yshift
取自将坐标附近的节点向上或向下移动
答案2
我会旋转图片并在条形侧面写上标签:
%\documentclass{article}
\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=66mm,
ybar stacked,
ymajorgrids = true,
xtick = {1,2},
xmin=0.5, xmax=3,
ymin=0, ymax=100,
yticklabel={\pgfmathprintnumber{\tick}\%},
%
nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%},
nodes near coords style={font=\scriptsize, anchor=west, xshift=1ex},
%
legend style = {at={(0.5,-0.12)},
anchor=north, font=\small,
legend columns=-1},
]
\addplot coordinates{(1, 20.00) (2, 22.60)};
\addplot coordinates{(1, 28.57) (2, 23.10)};
\addplot coordinates{(1, 11.43) (2, 12.14)};
\addplot coordinates{(1, 28.57) (2, 23.70)};
\addplot coordinates{(1, 6.43) (2, 8.07)};
\addplot coordinates{(1, 5.00) (2, 10.39)};
\legend{A,B,
A\textsubscript{1},B\textsubscript{1},
A\textsubscript{2},C}
\end{axis}
\end{tikzpicture}
\end{document}