Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

histfit

具有分布拟合的直方图

说明

示例

histfit(data) 绘制 data 中的值的直方图并拟合正态密度函数,直方图的 bin 个数等于 data 中元素个数的平方根。

示例

histfit(data,nbins) 使用 nbins 个 bin 绘制直方图,并拟合正态密度函数。

示例

histfit(data,nbins,dist) 使用 nbins 个 bin 绘制直方图,并根据 dist 指定的分布拟合密度函数。

示例

histfit(ax,___) 使用 Axes 对象 ax 指定的绘图坐标区。将 ax 指定为第一个输入参数,后跟先前语法中的任何输入参数组合。

示例

h = histfit(___) 返回句柄向量 h,其中 h(1) 是直方图的句柄,h(2) 是密度曲线的句柄。

示例

全部折叠

用均值 10 和方差 1 从正态分布生成大小为 100 的样本。

rng default; % For reproducibility
r = normrnd(10,1,100,1);

构建具有正态分布拟合的直方图。

histfit(r)

Figure contains an axes object. The axes object contains 2 objects of type bar, line.

histfit 使用 fitdist 对数据进行分布拟合。使用 fitdist 获得在拟合中使用的参数。

pd = fitdist(r,'Normal')
pd = 
  NormalDistribution

  Normal distribution
       mu = 10.1231   [9.89244, 10.3537]
    sigma =  1.1624   [1.02059, 1.35033]

参数估计值旁边的区间是分布参数的 95% 置信区间。

用均值 10 和方差 1 从正态分布生成大小为 100 的样本。

rng default; % For reproducibility
r = normrnd(10,1,100,1);

使用六个 bin 构造具有正态分布拟合的直方图。

histfit(r,6)

Figure contains an axes object. The axes object contains 2 objects of type bar, line.

使用参数 (3,10) 从 beta 分布生成大小为 100 的样本。

rng default;  % For reproducibility
b = betarnd(3,10,100,1);

使用 10 个 bin 构造具有 beta 分布拟合的直方图。

histfit(b,10,'beta')

Figure contains an axes object. The axes object contains 2 objects of type bar, line.

使用参数 (3,10) 从 beta 分布生成大小为 100 的样本。

rng default;  % For reproducibility
b = betarnd(3,10,[100,1]);

使用 10 个 bin 构造具有平滑函数拟合的直方图。

histfit(b,10,'kernel')

Figure contains an axes object. The axes object contains 2 objects of type bar, line.

用均值 3 和方差 1 从正态分布生成大小为 100 的样本。

rng('default') % For reproducibility
r = normrnd(3,1,100,1);

创建一个包含两个子图的图窗,并以 ax1ax2 形式返回 Axes 对象。通过引用对应的 Axes 对象,在每个坐标区中创建一个具有正态分布拟合的直方图。在左侧子图中,绘制一个具有 10 个 bin 的直方图。在右侧子图中,绘制一个具有 5 个 bin 的直方图。通过将对应的 Axes 对象传递给 title 函数,为每个绘图添加标题。

ax1 = subplot(1,2,1); % Left subplot
histfit(ax1,r,10,'normal')
title(ax1,'Left Subplot')

ax2 = subplot(1,2,2); % Right subplot
histfit(ax2,r,5,'normal')
title(ax2,'Right Subplot')

Figure contains 2 axes objects. Axes object 1 with title Left Subplot contains 2 objects of type bar, line. Axes object 2 with title Right Subplot contains 2 objects of type bar, line.

用均值 10 和方差 1 从正态分布生成大小为 100 的样本。

rng default % for reproducibility
r = normrnd(10,1,100,1);

构建具有正态分布拟合的直方图。

h = histfit(r,10,'normal')

Figure contains an axes object. The axes object contains 2 objects of type bar, line.

h = 
  2x1 graphics array:

  Bar
  Line

更改直方图的条形颜色。

h(1).FaceColor = [.8 .8 1];

Figure contains an axes object. The axes object contains 2 objects of type bar, line.

更改密度曲线的颜色。

h(2).Color = [.2 .2 .2];

Figure contains an axes object. The axes object contains 2 objects of type bar, line.

输入参数

全部折叠

输入数据,指定为向量。

示例: data = [1.5 2.5 4.6 1.2 3.4]

示例: data = [1.5 2.5 4.6 1.2 3.4]'

数据类型: double | single

直方图的 bin 个数,指定为正整数。默认值为 data 中元素数的平方根,向上舍入。在拟合分布时,使用 [ ] 表示使用默认的 bin 个数。

示例: y = histfit(x,8)

示例: y = histfit(x,10,'gamma')

示例: y = histfit(x,[ ],'weibull')

数据类型: double | single

要与直方图拟合的分布,指定为字符向量或字符串标量。下表显示支持的分布。

dist描述
'beta'beta
'birnbaumsaunders'伯恩鲍姆-桑德斯
'burr'伯尔 XII 型
'exponential'指数
'extreme value''ev'极值
'gamma'gamma
'generalized extreme value''gev'广义极值
'generalized pareto''gp'广义帕累托(阈值 0)
'inversegaussian'逆高斯
'logistic'逻辑
'loglogistic'对数逻辑
'lognormal'对数正态
'nakagami'Nakagami
'negative binomial''nbin'负二项
'normal'正态
'poisson'泊松
'rayleigh'瑞利
'rician'莱斯
'tlocationscale't 位置尺度
'weibull''wbl'威布尔
'kernel'非参数化核平滑分布。密度在覆盖 data 数据范围的 100 个等间距点处进行计算。它最适合连续分布的样本。

绘图的坐标区,指定为 Axes 对象。如果未指定 ax,则 histfit 使用当前坐标区创建绘图。有关创建 Axes 对象的详细信息,请参阅 axes

输出参数

全部折叠

以向量形式返回的绘图句柄,其中 h(1) 是直方图句柄,h(2) 是密度曲线句柄。histfit 对密度进行归一化,使曲线下的总面积与直方图的总面积相匹配。

算法

histfit 使用 fitdist 对数据进行分布拟合。使用 fitdist 获得在拟合中使用的参数。

版本历史记录

在 R2006a 之前推出