主要内容

histfit

R2026b

具有分布拟合的直方图

描述

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

示例

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

示例

histfit(data,nbins,dist) 使用 nbins 个箱绘制直方图,并根据 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);

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

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 个箱构造具有 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 个箱构造具有平滑函数拟合的直方图。

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);

创建一个包含两个子图的图窗,并以 ax1 和 ax2 形式返回 Axes 对象。通过引用对应的 Axes 对象,在每个坐标区中创建一个具有正态分布拟合的直方图。在左侧子图中,绘制一个具有 10 个箱的直方图。在右侧子图中,绘制一个具有 5 个箱的直方图。通过将对应的 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 = 
  2×1 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

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

示例: y = histfit(x,8)

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

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

数据类型: double | single

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

分布名称描述分布对象
'Beta'beta 分布BetaDistribution
'BirnbaumSaunders'伯恩鲍姆-桑德斯分布BirnbaumSaundersDistribution
'Burr'伯尔分布BurrDistribution
'Exponential'指数分布ExponentialDistribution
'Extreme Value' 或 'ev'极值分布ExtremeValueDistribution
'Gamma'gamma 分布GammaDistribution
'Generalized Extreme Value' 或 'gev'广义极值分布GeneralizedExtremeValueDistribution
'Generalized Pareto' 或 'gp'广义帕累托分布GeneralizedParetoDistribution
'Half Normal' 或 'hn'半正态分布HalfNormalDistribution
'InverseGaussian'逆高斯分布InverseGaussianDistribution
'Kernel'非参数化核平滑分布。密度在覆盖数据范围的 100 个等间距点处进行计算。它最适合连续分布的样本。KernelDistribution
'Logistic'逻辑分布LogisticDistribution
'Loglogistic'对数逻辑分布LoglogisticDistribution
'Lognormal'对数正态分布LognormalDistribution
'Nakagami'Nakagami 分布NakagamiDistribution
'Negative Binomial' 或 'nbin'负二项分布NegativeBinomialDistribution
'Normal'正态分布NormalDistribution
'Poisson'泊松分布PoissonDistribution
'Rayleigh'瑞利分布RayleighDistribution
'Rician'莱斯分布RicianDistribution
'Stable'稳定分布StableDistribution
'tLocationScale't 位置尺度分布tLocationScaleDistribution
'Weibull' 或 'wbl'威布尔分布WeibullDistribution

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

输出参量

全部折叠

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

算法

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

扩展功能

全部展开

版本历史记录

在 R2006a 之前推出

另请参阅

| | |