我正在尝试绘制一个宽度为 10 的 8 个箱的直方图。这是我目前所拥有的,其中我的箱宽明显小于 10 个单位。
\begin{tikzpicture}
\begin{axis} [ybar,xmin=70,xmax=160,ymin=0,ymax=20, xlabel = IQ Scores, ylabel = Frequency, xtick={70, 80, 90, 100, 110, 120, 130, 140, 150}]
\addplot [draw = black, fill = gray] coordinates {
(75,2)
(85,3)
(95, 10)
(105, 16)
(115, 13)
(125, 10)
(135, 5)
(145,1)
};
\end{axis}
\end{tikzpicture}
我基本上需要让这些杆相互接触。有什么想法我需要做什么吗?
答案1
其中一种方法是使用ybar interval
(注意 x 轴上刻度位置的细微差别)
\begin{tikzpicture}
\begin{axis} [ybar interval,
grid=none,
xmin=70,xmax=160,ymin=0,ymax=20, xlabel = IQ Scores, ylabel = Frequency, xtick={70, 80, 90, 100, 110, 120, 130, 140, 150}]
\addplot [draw = black, fill = gray] coordinates {
(75,2)
(85,3)
(95, 10)
(105, 16)
(115, 13)
(125, 10)
(135, 5)
(145,1)
(155,0)
};
\end{axis}
\end{tikzpicture}
答案2
您可以设置 x 轴的单位宽度(例如x=0.1cm
)和条形宽度(bar width=1cm
因为您的步长是 10)。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
ybar,
x=0.1cm,
bar width=1cm,
xmin=70,
xmax=160,
ymin=0,
ymax=20,
xlabel=IQ Scores,
ylabel=Frequency,
xtick={70, 80, 90, 100, 110, 120, 130, 140, 150}
]
\addplot [draw = black, fill = gray] coordinates {
(75,2)
(85,3)
(95, 10)
(105, 16)
(115, 13)
(125, 10)
(135, 5)
(145,1)
};
\end{axis}
\end{tikzpicture}
\end{document}