这是我的代码:
\begin{figure}[h]
\caption{Test Caption}
\centering
\begin{tikzpicture}[
every axis/.style={
ybar stacked,
ymin=0,ymax=100,
ymajorgrids = true,
x tick label style={
% add some negative yshift to move ticklabels down
yshift=-10mm,
xshift=5mm,
%rotate=45,
anchor=east},
x axis line style = { opacity = 0 },
y axis line style = { opacity = 0 },
tickwidth = 0pt,
width=8cm,
height=6cm,
% symbolic coords have numerical distance of 1
% so with the following line you get a tick at every symbolic coord
xtick distance=1,
symbolic x coords={
A,
B,
C,
},
bar width=8pt
},
]
\pgfplotsset{
% define a new style used for the plot used to add labels
% 2 args means it takes two mandatory arguments, so must be used as
% labelplot={first arg}{second arg}
labelplot/.style 2 args={
% forget plot means it doesn't affect cycle lists or legends
forget plot,
% #1 is first argument, the text used in the nodes near coords
nodes near coords=#1,
% #2 is second argument, a length that should be the same as the bar shift for the axis
every node near coord/.style={below,font=\small,xshift=#2,anchor=east,rotate=90}
}
}
\begin{axis}[bar shift=-10pt, xticklabels={}]
\addplot [labelplot={1st}{-10pt}] coordinates
{(A,0) (B,0) (C,0)};
\addplot coordinates
{(A,64) (B,64) (C,48)};
\addplot coordinates
{(A,12) (B,19) (C,33)};
\addplot coordinates
{(A,24) (B,17) (C,33)};
\end{axis}
\begin{axis}
\addplot [labelplot={2nd}{0pt}] coordinates
{(A,0) (B,0) (C,0)};
\addplot+[fill=blue!50!gray] coordinates
{(A,53) (B,28) (C,38)};
\addplot+[fill=red!50!gray] coordinates
{(A,18) (B,19) (C,19)};
\addplot+[fill=yellow!50!gray] coordinates
{(A,30) (B,53) (C,43)};
\end{axis}
\begin{axis}[bar shift=10pt]
\addplot [labelplot={3rd}{10pt}] coordinates
{(A,0) (B,0) (C,0)};
\addplot+[fill=blue!50!gray] coordinates
{(A,42) (B,25) (C,33)};
\addplot+[fill=red!50!gray] coordinates
{(A,19) (B,27) (C,20)};
\addplot+[fill=yellow!50!gray] coordinates
{(A,39) (B,48) (C,47)};
\end{axis}
\begin{axis}[bar shift=20pt,xticklabels={}]
\addplot [labelplot={4th}{20pt}] coordinates
{(A,0) (B,0) (C,0)};
\addplot+[fill=blue!50!gray] coordinates
{(A,42) (B,25) (C,40)};
\addplot+[fill=red!50!gray] coordinates
{(A,19) (B,27) (C,20)};
\addplot+[fill=yellow!50!gray] coordinates
{(A,39) (B,48) (C,40)};
\end{axis}
\end{tikzpicture}
\end{figure}
答案1
- 使用您的代码节点时,每个堆栈条下方的节点不会像您在问题中的图像上显示的那样显示,但这不是您的问题:-)
- 通过添加
enlarge x limits=0.25,
每个轴样式并相应地更改xshift
条形位置,您可以获得:
与您的相比,下面的 MWE(最小工作示例)的变化标记为% <---
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{figure}
\caption{Test Caption}
\centering
\begin{tikzpicture}[
every axis/.style={
width=8cm, height=6cm,
ybar stacked,
bar width=8pt,
xtick style = {draw=none}, %<---
x tick label style={yshift=-10mm}, %<---
ymajorgrids = true,
ymin=0,ymax=100,
% symbolic coords have numerical distance of 1
% so with the following line you get a tick at every symbolic coord
xtick distance=1,
symbolic x coords={A, B, C},
enlarge x limits=0.25, % <--- added
}]
\pgfplotsset{
% define a new style used for the plot used to add labels
% 2 args means it takes two mandatory arguments, so must be used as
% labelplot={first arg}{second arg}
labelplot/.style 2 args={
% forget plot means it doesn't affect cycle lists or legends
%forget plot,
% #1 is first argument, the text used in the nodes near coords
nodes near coords=#1,
% #2 is second argument, a length that should be the same as the bar shift for the axis
every node near coord/.style={below,font=\small,xshift=#2,anchor=east,rotate=90},
}
}
\begin{axis}[bar shift=-15pt] % <--- changed
\addplot [labelplot={1st}{15pt}] coordinates {(A,0) (B,0) (C,0)};
\addplot coordinates {(A,64) (B,64) (C,48)};
\addplot coordinates {(A,12) (B,19) (C,33)};
\addplot coordinates {(A,24) (B,17) (C,33)};
\end{axis}
\begin{axis}[bar shift=-5pt] % <--- changed
\addplot [labelplot={2nd}{0pt}] coordinates {(A,0) (B,0) (C,0)};
\addplot+[fill=blue!50!gray] coordinates {(A,53) (B,28) (C,38)};
\addplot+[fill=red!50!gray] coordinates {(A,18) (B,19) (C,19)};
\addplot+[fill=yellow!50!gray] coordinates {(A,30) (B,53) (C,43)};
\end{axis}
\begin{axis}[bar shift=5pt] % <--- changed
\addplot [labelplot={3rd}{10pt}] coordinates {(A,0) (B,0) (C,0)};
\addplot+[fill=blue!50!gray] coordinates {(A,42) (B,25) (C,33)};
\addplot+[fill=red!50!gray] coordinates {(A,19) (B,27) (C,20)};
\addplot+[fill=yellow!50!gray] coordinates {(A,39) (B,48) (C,47)};
\end{axis}
\begin{axis}[bar shift=15pt] % <--- changed
\addplot [labelplot={4th}{20pt}] coordinates {(A,0) (B,0) (C,0)};
\addplot+[fill=blue!50!gray] coordinates {(A,42) (B,25) (C,40)};
\addplot+[fill=red!50!gray] coordinates {(A,19) (B,27) (C,20)};
\addplot+[fill=yellow!50!gray] coordinates {(A,39) (B,48) (C,40)};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
附录: 这是一个解决方案:
- 合并你的
tikzpicture
样式选项every axis/.style
和样式设置与\pgfplotsset
在简洁明了的常见\pgfplotsset
插入后\begin{tikzpicture}
- 被添加到所有
axis
选项设置中bar width
,bar shift
- 通过将 选项添加到最后三个 选项中,消除了多次覆盖
xtick
和标签yticks
ytick=\empty, xtick=\empty
axis
- 通过添加代码行,向每个条形堆栈添加标签 1、2、3 和 4:
point meta=explicit symbolic,
stacked ignores zero=false
到 中的设置\pgfplotsset
。
\addplot
堆栈的标签作为第一个坐标的选项写入
上述变化在代码中用 标记% <---
,而用于标记条形堆栈的添加的代码行则用 标记% <===
:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{figure}
\caption{Test Caption}
\centering
\begin{tikzpicture}
\pgfplotsset{
width=8cm, height=6cm,
enlarge x limits=0.25, % <---
xtick style = {draw=none}, % <---
x tick label style={yshift=-2em}, % <---
ytick={0,20,...,100},
% coordinates
xtick distance=1,
symbolic x coords={A, B, C},
ymin=0, ymax=100,
ybar stacked,
axis on top, % <---
% nodes
nodes near coords,
every node near coord/.style={font=\footnotesize,
below, rotate=90, anchor=east}, % <---
point meta=explicit symbolic, % <===
stacked ignores zero=false % <===
}
%%%% diagram body
\begin{axis}[bar width=8pt,
bar shift=-15pt]
\addplot [xshift=-15pt] coordinates
{(A,0) [1st]
(B,0) [1st]
(C,0) [1st]
};
\addplot coordinates {(A,52) (B,28) (C,38)};
\addplot coordinates {(A,18) (B,19) (C,19)};
\addplot coordinates {(A,30) (B,53) (C,43)};
\end{axis}
\begin{axis}[ytick=\empty, xtick=\empty,
bar width=8pt,
bar shift=-5pt]
\addplot [xshift=-5pt] coordinates
{(A,0) [2nd]
(B,0) [2nd]
(C,0) [2nd]
};
\addplot [fill=blue!50!gray] coordinates {(A,52) (B,28) (C,38)};
\addplot [fill=red!50!gray] coordinates {(A,18) (B,19) (C,19)};
\addplot [fill=yellow!50!gray] coordinates {(A,30) (B,53) (C,43)};
\end{axis}
\begin{axis}[ytick=\empty, xtick=\empty,
bar width=8pt,
bar shift= 5pt]
\addplot [xshift=5pt] coordinates
{(A,0) [3rd]
(B,0) [3rd]
(C,0) [3rd]
};
\addplot [fill=blue!50!gray] coordinates {(A,42) (B,25) (C,33)};
\addplot [fill=red!50!gray] coordinates {(A,19) (B,27) (C,20)};
\addplot [fill=yellow!50!gray] coordinates {(A,39) (B,48) (C,47)};
\end{axis}
\begin{axis}[ytick=\empty, xtick=\empty,
bar width=8pt,
bar shift=15pt]
\addplot [xshift=15pt] coordinates
{(A,0) [4th]
(B,0) [4th]
(C,0) [2nd]
};
\addplot [fill=blue!50!gray] coordinates {(A,42) (B,25) (C,40)};
\addplot [fill=red!50!gray] coordinates {(A,19) (B,27) (C,20)};
\addplot [fill=yellow!50!gray] coordinates {(A,39) (B,48) (C,40)};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}