我正在尝试制作分组堆叠条形图。我已经做到了这一点,代码如下。我的问题是应该有三个条形图组 - 但此代码似乎只生成了两个条形图组。此外,我需要两组标签 - 一组用于种族,一组用于世代,但不确定如何实现这一点,尤其是考虑到当前标签不起作用。任何帮助都感激不尽。
非常感谢,波莉
\begin{tikzpicture}
\begin{axis}[ybar stacked,bar shift=8pt,ymin=0,ymax=11, x tick label
style={rotate=45,anchor=east},symbolic x coords={White Other,African
Caribbean and White,African,D,E}]]
\addplot coordinates
{(White Other,1) (African Caribbean and White,1) (African,3) (D,2) (E,1.5)};
\addplot coordinates
{(White Other,1) (African Caribbean and White,1) (African,3) (D,2) (E,1.5)};
\addplot coordinates
{(White Other,1) (African Caribbean and White,1) (African,3) (D,2) (E,1.5)};
\end{axis}
\begin{axis}[ybar stacked,bar shift=-8pt,ymin=0,ymax=11,symbolic x coords=
{White Other,African Caribbean and White,African,D,E}]
\addplot+[fill=blue!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5)
(E,1)};
\addplot+[fill=red!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5)
(E,1)};
\addplot+[fill=yellow!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5)
(E,1)};
\end{axis}
\begin{axis}[ybar stacked,bar shift=-8pt,ymin=0,ymax=11,symbolic x coords=
{White Other,African Caribbean and White,African,D,E}]
\addplot+[fill=blue!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5)
(E,1)};
\addplot+[fill=red!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5)
(E,1)};
\addplot+[fill=yellow!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5)
(E,1)};
\end{axis}
\end{tikzpicture}
答案1
问题在于,你bar shift
对两个axis
环境使用了相同的值。例如,你想要-10pt
一个环境使用 0 axis
,下一个环境使用 0,10pt
最后一个环境使用 0。
我可能还会添加hide axis
前两个axis
环境,以便轴框、刻度和刻度标签仅绘制一次。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}[
every axis/.style={ % add these settings to all the axis environments in the tikzpicture
ybar stacked,
ymin=0,ymax=11,
x tick label style={rotate=45,anchor=east},
symbolic x coords={
White Other,
African Caribbean and White,
African,D,E
},
bar width=8pt
},
]
% bar shift -10pt here
\begin{axis}[bar shift=-10pt,hide axis]
\addplot coordinates
{(White Other,1) (African Caribbean and White,1) (African,3) (D,2) (E,1.5)};
\addplot coordinates
{(White Other,1) (African Caribbean and White,1) (African,3) (D,2) (E,1.5)};
\addplot coordinates
{(White Other,1) (African Caribbean and White,1) (African,3) (D,2) (E,1.5)};
\end{axis}
% zero bar shift here
\begin{axis}[hide axis]
\addplot+[fill=blue!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5)
(E,1)};
\addplot+[fill=red!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5)
(E,1)};
\addplot+[fill=yellow!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5)
(E,1)};
\end{axis}
% and bar shift +10pt here
\begin{axis}[bar shift=10pt]
\addplot+[fill=blue!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5)
(E,1)};
\addplot+[fill=red!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5)
(E,1)};
\addplot+[fill=yellow!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5)
(E,1)};
\end{axis}
\end{tikzpicture}
\end{document}