我正在努力在复杂的堆叠和分组条形图上正确标注标签。我想用一个民族来标记每组条形图,并且我想在每组条形图中将三个单独的条形图标记为第一、第二和第三。任何关于如何做到这一点的建议都欢迎。
\begin{figure}[h]
\caption{Graph showing the percentage of births in different partnership
scenarios to women, by generation and ethnic group, 1986-2014}
\centering
\begin{tikzpicture}[
every axis/.style={
ybar stacked,
ymin=0,ymax=100,
x tick label style={rotate=45,anchor=east},
x axis line style = { opacity = 0 },
y axis line style = { opacity = 0 },
tickwidth = 0pt,
width=17cm,
height=8cm,
symbolic x coords={
African,
African and White,
African Caribbean,
African Caribbean and White,
Indian,
Pakistani,
Bangladeshi,
Asian and White,
},
bar width=6pt
},
]
\begin{axis}[bar shift=-12pt, xticklabels={}]
\addplot coordinates
{(African,64) (African and White,64) (African Caribbean,48) (African
Caribbean and White,47) (Indian, 98) (Pakistani, 98) (Bangladeshi, 99)
(Asian and White, 84)};
\addplot coordinates
{(African,12) (African and White,19) (African Caribbean,33) (African
Caribbean and White,22) (Indian,1) (Pakistani, 1) (Bangladeshi, 0) (Asian
and White, 11)};
\addplot coordinates
{(African,24) (African and White,17) (African Caribbean,33) (African
Caribbean and White,31) (Indian, 1) (Pakistani, 1) (Bangladeshi, 1) (Asian
and White, 5)};
\end{axis}
\begin{axis}
\addplot+[fill=blue!50!gray] coordinates
{(African,53) (African and White,28) (African Caribbean,38) (African
Caribbean and White,34) (Indian, 92) (Pakistani, 93) (Bangladeshi, 91)
(Asian and White, 69)};
\addplot+[fill=red!50!gray] coordinates
{(African,18) (African and White,19) (African Caribbean,19) (African
Caribbean and White,28) (Indian,4) (Pakistani, 3) (Bangladeshi, 5) (Asian
and White, 20)};
\addplot+[fill=yellow!50!gray] coordinates
{(African,30) (African and White,53) (African Caribbean,43) (African
Caribbean and White,38) (Indian, 4) (Pakistani, 4) (Bangladeshi, 4) (Asian
and White, 11)};
\end{axis}
\begin{axis}[bar shift=12pt,xticklabels={}]
\addplot+[fill=blue!50!gray] coordinates
{(African,42) (African and White,25) (African Caribbean,33) (African
Caribbean and White,29) (Indian,88) (Pakistani, 85) (Asian and White, 50)};
\addplot+[fill=red!50!gray] coordinates
{(African,19) (African and White,27) (African Caribbean,20) (African
Caribbean and White,27) (Indian, 5) (Pakistani, 6) (Asian and White, 26)};
\addplot+[fill=yellow!50!gray] coordinates
{(African,39) (African and White,48) (African Caribbean,47) (African
Caribbean and White,44) (Indian, 7) (Pakistani, 9) (Asian and White, 25)};
\end{axis}
\end{tikzpicture}
\end{figure}
答案1
要为每个组获取单个标签,请设置xtick distance=1
。对于条形下方的标签,您可以添加一个带有零高度条形的图,并使用nodes near coords={some text}
添加标签,同时every node near coord/.style={below}
将标签移动到条形下方。
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{figure}[h]
\caption{Graph showing the percentage of births in different partnership
scenarios to women, by generation and ethnic group, 1986-2014}
\centering
\begin{tikzpicture}[
every axis/.style={
ybar stacked,
ymin=0,ymax=100,
x tick label style={
% add some negative yshift to move ticklabels down
yshift=-4mm,
rotate=45,
anchor=east},
x axis line style = { opacity = 0 },
y axis line style = { opacity = 0 },
tickwidth = 0pt,
width=15cm,
height=8cm,
% 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={
African,
African and White,
African Caribbean,
African Caribbean and White,
Indian,
Pakistani,
Bangladeshi,
Asian and White,
},
bar width=6pt
},
]
\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=\tiny,xshift=#2}
}
}
\begin{axis}[bar shift=-12pt, xticklabels={}]
\addplot [labelplot={1st}{-12pt}] coordinates
{(African,0) (African and White,0) (African Caribbean,0) (African
Caribbean and White,0) (Indian, 0) (Pakistani, 0) (Bangladeshi, 0)
(Asian and White, 0)};
\addplot coordinates
{(African,64) (African and White,64) (African Caribbean,48) (African
Caribbean and White,47) (Indian, 98) (Pakistani, 98) (Bangladeshi, 99)
(Asian and White, 84)};
\addplot coordinates
{(African,12) (African and White,19) (African Caribbean,33) (African
Caribbean and White,22) (Indian,1) (Pakistani, 1) (Bangladeshi, 0) (Asian
and White, 11)};
\addplot coordinates
{(African,24) (African and White,17) (African Caribbean,33) (African
Caribbean and White,31) (Indian, 1) (Pakistani, 1) (Bangladeshi, 1) (Asian
and White, 5)};
\end{axis}
\begin{axis}
\addplot [labelplot={2nd}{0pt}] coordinates
{(African,0) (African and White,0) (African Caribbean,0) (African
Caribbean and White,0) (Indian, 0) (Pakistani, 0) (Bangladeshi, 0)
(Asian and White, 0)};
\addplot+[fill=blue!50!gray] coordinates
{(African,53) (African and White,28) (African Caribbean,38) (African
Caribbean and White,34) (Indian, 92) (Pakistani, 93) (Bangladeshi, 91)
(Asian and White, 69)};
\addplot+[fill=red!50!gray] coordinates
{(African,18) (African and White,19) (African Caribbean,19) (African
Caribbean and White,28) (Indian,4) (Pakistani, 3) (Bangladeshi, 5) (Asian
and White, 20)};
\addplot+[fill=yellow!50!gray] coordinates
{(African,30) (African and White,53) (African Caribbean,43) (African
Caribbean and White,38) (Indian, 4) (Pakistani, 4) (Bangladeshi, 4) (Asian
and White, 11)};
\end{axis}
\begin{axis}[bar shift=12pt,xticklabels={}]
\addplot [labelplot={3rd}{12pt}] coordinates
{(African,0) (African and White,0) (African Caribbean,0) (African
Caribbean and White,0) (Indian, 0) (Pakistani, 0) (Bangladeshi, 0)
(Asian and White, 0)};
\addplot+[fill=blue!50!gray] coordinates
{(African,42) (African and White,25) (African Caribbean,33) (African
Caribbean and White,29) (Indian,88) (Pakistani, 85) (Asian and White, 50)};
\addplot+[fill=red!50!gray] coordinates
{(African,19) (African and White,27) (African Caribbean,20) (African
Caribbean and White,27) (Indian, 5) (Pakistani, 6) (Asian and White, 26)};
\addplot+[fill=yellow!50!gray] coordinates
{(African,39) (African and White,48) (African Caribbean,47) (African
Caribbean and White,44) (Indian, 7) (Pakistani, 9) (Asian and White, 25)};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}