\documentclass{ctexart}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{every picture/.style={>=latex}}

%坐标轴设定
\newcommand\plzj{$\dfrac{\text{频率}}{\text{组距}}$}
\newcommand\zftw{--++(0.2,0)
--++(0.05,0.15)--++(0.1,-0.3)--++(0.05,0.15)
--++(0.05,0.15)--++(0.1,-0.3)--++(0.05,0.15)
--++(0.05,0.15)--++(0.1,-0.3)--++(0.05,0.15)
--++(0.2,0)}
\newcommand\zftzb[6]{%
\draw[<->] (0,#3/\zftyb)node[right]{\plzj}
--(0,0)node[below]{0} \zftw --(#1,0)node[above]{#2};
\foreach \i in {1,2,...,#6} {
\node[below] at (\i,0) {\pgfmathparse{int(#4+#5*(\i-1))}\pgfmathresult};
}}
%用法：
%\zftzb{x轴长}{x轴标}{y轴长}{x轴刻度开始}{组距}{刻度个数}
%此处y轴长非实际长度，而是比最高的直方数值大一些即可

%画直方命令
\newcount\zftctr
\newcommand\zftyb{1}%y轴的比例
\makeatletter
\newcommand\zft{\@ifstar{\zftb}{\zfta}}
\makeatother
\newcommand\zfta[2][]{\advance\zftctr by 1\relax
\draw (\zftctr,#2/\zftyb) rectangle (\zftctr+1,0);
\draw[dashed] (0,#2/\zftyb)node[left,#1]{#2}--(\zftctr,#2/\zftyb);
}
\newcommand\zftb[1]{\advance\zftctr by 1\relax
\draw (\zftctr,#1/\zftyb) rectangle (\zftctr+1,0);
}
%用法：
%用\zft{...}画直方，无需标y值的用\zft*{...}，
%调节数字位置用\zft[shift={(...)}]{...}

\newcommand\zftkz{%靠左式，即组从0开始时用
\zftctr=-1\relax\def\zftw{}}


%实例
\begin{document}

\begin{tikzpicture}
\def\zftyb{0.008}%设定y轴比例，一定要先设
\zftzb{8}{分数}{0.036}{40}{10}{7}
%\zftzb{x长}{x标}{y长}{刻度始}{组距}{刻度数}
\zft{0.005}
\zft*{0.015}
\zft{0.030}
\zft{0.025}
\zft{0.015}
\zft{0.010}
\end{tikzpicture}

\begin{tikzpicture}
\def\zftyb{0.01}
\zftkz
\zftzb{6}{指标}{0.045}{10}{10}{5}
\zft[shift={(0,-2pt)}]{0.006}
\zft{0.026}
\zft{0.038}
\zft{0.022}
\zft[shift={(0,2pt)}]{0.008}
\end{tikzpicture}

\end{document}
