条形图中缺少条形

条形图中缺少条形

我正在尝试绘制一个条形图,其中每个独立变量都有多个条形图显示其在不同领域的表现。我在这里遇到的问题是第三个变量的条形图没有显示出来。我使用的代码如下所示。

\begin{center}
\begin{tikzpicture}
\begin{axis}[
x tick label style={
/pgf/number format/1000 sep=},
ylabel=Passenger Count,
enlargelimits=0.05,
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=-1},
ybar interval=0.5,
symbolic x coords={1st Class, 2nd Class, 3rd Class},
grid=major
]
\addplot coordinates {(1st Class,6) (2nd Class,6) (3rd Class,324)};
\addplot coordinates {(1st Class,0) (2nd Class,98) (3rd Class,81)};
\addplot coordinates {(1st Class,41) (2nd Class,56) (3rd Class,45)};
\addplot coordinates {(1st Class,169) (2nd Class,24) (3rd Class,41)};
\legend{0-10,10-20,20-30,$>30$}
\end{axis}
\end{tikzpicture}
\end{center}

答案1

发生这种情况的原因是您正在使用ybar interval:此样式要求每个条形的 x 坐标都有一个起始值和终止值,因此如果您有三个 x 坐标,则只会绘制两个条形。在包含多个系列的图中,这种样式通常没有多大意义。请ybar改用:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
ylabel=Passenger Count,
enlarge x limits=0.2,
legend style={
    at={(0.5,-0.15)},
    anchor=north,legend columns=-1
},
ymin=0,
ybar,
xtick=data,
symbolic x coords={1st Class, 2nd Class, 3rd Class},
grid=major,
xmajorgrids=false
]
\addplot coordinates {(1st Class,6) (2nd Class,6) (3rd Class,324)};
\addplot coordinates {(1st Class,0) (2nd Class,98) (3rd Class,81)};
\addplot coordinates {(1st Class,41) (2nd Class,56) (3rd Class,45)};
\addplot coordinates {(1st Class,169) (2nd Class,24) (3rd Class,41)};
\legend{0-10,10-20,20-30,$>30$}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

相关内容