Main Content

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

histogram

直方图

  • Histogram plot

说明

直方图是一种条形图,它将数据分组为 bin。创建 Histogram 对象后,可以通过更改直方图的属性值修改它的各个方面。这对快速修改 bin 属性或更改显示特别有用。

创建对象

描述

示例

histogram(X) 基于 X 创建直方图。histogram 函数使用自动分 bin 算法,然后返回均匀宽度的 bin,这些 bin 可涵盖 X 中的元素范围并显示分布的基本形状。histogram 将 bin 显示为矩形条,这样每个矩形的高度就表示 bin 中的元素数量。

示例

histogram(X,nbins) 指定 bin 的数量。

示例

histogram(X,edges)X 划分为在向量中指定 bin 边界的 bin。

histogram('BinEdges',edges,'BinCounts',counts) 绘制指定的 bin 计数,而不执行任何数据分 bin。

示例

histogram(C) 通过为分类数组 C 中的每个类别绘制一个条形来绘制直方图。

histogram(C,Categories) 只绘制 C 中类别的子集。

histogram('Categories',Categories,'BinCounts',counts) 手动指定类别和关联的 bin 计数。histogram 绘制指定的 bin 计数,而不执行任何数据分 bin。

示例

histogram(___,Name,Value) 使用一个或多个名称-值参量为上述任一语法指定其他参量。例如,指定 Normalization 以使用不同类型的归一化。有关属性列表,请参阅 Histogram 属性

histogram(ax,___) 在指定的坐标区中而不是当前坐标区 (gca) 中绘图。ax 可以位于上述语法中的任何输入参量组合之前。

示例

h = histogram(___) 返回 Histogram 对象。使用此语法可检查并调整直方图的属性。有关属性列表,请参阅 Histogram 属性

输入参量

全部展开

要分布到各 bin 的数据,指定为向量、矩阵或多维数组。histogram 将矩阵和多维数组数据视为单个列向量 X(:),并绘制单个直方图。

histogram 忽略所有 NaNNaT 值。同样,histogram 忽略 Inf-Inf 值,除非 bin 边界将 Inf-Inf 显式指定为 bin 边界。虽然 NaNNaTInf-Inf 值通常情况下不会绘制,但在包括所有数据元素的归一化计算(如 'probability')中,仍然会包含这些值。

注意

如果 X 包含类型为 int64uint64 且大于 flintmax 的整数,则建议您显式指定直方图 bin 边界。histogram 会自动使用双精度对输入数据进行分 bin,这些数据缺少大于 flintmax 的数字的整数精度。

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | datetime | duration

分类数据,指定为分类数组。histogram 不会绘制未定义的分类值。但在包括所有数据元素的归一化计算(如 'probability')中,仍然会包含未定义的分类值。

数据类型: categorical

bin 数量,指定为正整数。如果未指定 nbins,则 histogram 根据 X 中的值确定 bin 的数量。

如果用 BinMethodBinWidthBinEdges 指定 nbins,则 histogram 仅采用最后一个参数。

示例: histogram(X,15) 创建一个带 15 个 bin 的直方图。

bin 边界,指定为向量。edges(1) 是第一个 bin 的左边界,edges(end) 是最后一个 bin 的右边界。

每个 bin 都包含左边界,但不包含右边界,除了同时包含两个边界的最后一个 bin 外。

对于 datetimeduration 数据,edges 必须为单调递增顺序的 datetimeduration 向量。

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | datetime | duration

注意

此选项仅适用于分类直方图。

直方图中包含的类别,指定为字符向量元胞数组、分类数组、字符串数组或 pattern 标量。

  • 如果您指定输入分类数组 C,则默认情况下,histogram 将为 C 中的每个类别绘制一个条形。在这种情况下,请改用 Categories 以指定类别的唯一子集。

  • 如果您指定 bin 计数,Categories 将为直方图指定关联的类别名称。

示例: h = histogram(C,{'Large','Small'}) 只绘制类别 'Large''Small' 中的分类数据。

示例: histogram(C,"Y" + wildcardPattern) 对名称以字母 Y 开头的类别中的数据进行绘图。

示例: histogram('Categories',{'Yes','No','Maybe'},'BinCounts',[22 18 3]) 绘制包含三个类别及关联 bin 计数的直方图。

示例: h.Categories 查询直方图对象 h 中的类别。

数据类型: cell | categorical | string | pattern

bin 计数,指定为向量。当单独执行 bin 计数计算且不希望 histogram 执行任何数据分 bin 时,可使用此输入将 bin 计数传递给 histogram

counts 的大小必须等于 bin 的数量。

  • 对于数值直方图,bin 的数量为 length(edges)-1

  • 对于分类直方图,bin 的数量等于类别的数量。

示例: histogram('BinEdges',-2:2,'BinCounts',[5 8 15 9])

示例: histogram('Categories',{'Yes','No','Maybe'},'BinCounts',[22 18 3])

目标坐标区,指定为 Axes 对象或 PolarAxes 对象。如果您不指定坐标区而且当前坐标区是笛卡尔坐标区,histogram 函数将使用当前坐标区 (gca)。要在极坐标区上绘图,请指定 PolarAxes 对象作为第一个输入参量,或者使用 polarhistogram 函数。

名称-值参数

将可选的参量对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但参量对组的顺序无关紧要。

示例: histogram(X,BinWidth=5)

在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。

示例: histogram(X,'BinWidth',5)

注意

此处所列的属性只是一部分。有关完整列表,请参阅 Histogram 属性

bin

全部展开

bin 的宽度,指定为正标量。如果指定 BinWidth,则 Histogram 最多可以使用 65,536(即 216)个 bin。如果指定的 bin 宽度需要多个 bin,则 histogram 使用与最大 bin 数对应的较大的 bin 宽度。

  • 对于 datetimeduration 数据,BinWidth 可以是标量持续时间或日历持续时间。

  • 如果用 BinMethodNumBinsBinEdges 指定 BinWidth,则 histogram 仅采用最后一个参数。

  • 此选项不适用于分类数据。

示例: histogram(X,'BinWidth',5) 使用宽度为 5 的 bin。

bin 范围,指定为二元素向量 [bmin,bmax]。第一个元素指示第一个 bin 边界。第二个元素指示最后一个 bin 边界。

此选项仅使用在 bin 范围 X>=bmin & X<=bmax(包含这两个边界值)内的数据进行计算。

此选项不适用于分类数据。

示例: histogram(X,'BinLimits',[1,10]) 仅包括 X 中介于 110(含二者)之间的值。

bin 范围的选择模式,指定为 'auto''manual'。默认值是 'auto',这样 bin 范围自动调整为该数据。

  • 如果指定 BinLimitsBinEdges,则 BinLimitsMode 设置为 'manual'。将 BinLimitsMode 指定为 'auto' 可将 bin 范围重新调整为该数据。

  • 此选项不适用于分类数据的直方图。

分 bin 算法,指定为此表中的一个值。

描述

'auto'

默认的 'auto' 算法选择一个 bin 宽度,以便涵盖数据范围并显示基础分布形状。

'scott'

如果数据接近正态分布,则 Scott 规则最佳。该规则也适用于大多数的其他分布。它使用 bin 宽度 3.5*std(X(:))*numel(X)^(-1/3)

'fd'

Freedman-Diaconis 规则对数据中的离群值不太敏感,可能更适用于重尾分布的数据。它使用 bin 宽度 2*iqr(X(:))*numel(X)^(-1/3)

'integers'

整数规则对整数数据有用,因为它为每个整数创建一个 bin。它使用 bin 宽度 1 并将 bin 边界放在整数的中间。

为避免无意间创建太多 bin,可以使用该规则创建 65536 (216) 个 bin 的限制。如果数据范围大于 65536,则整数规则改用更宽的 bin。

'integers' 不支持日期时间和持续时间数据。

'sturges'

Sturges 规则因其简单性而广受欢迎。它将 bin 数量选择为 ceil(1 + log2(numel(X)))

'sqrt'

平方根规则是广泛用于其他软件包。它将 bin 数量选择为 ceil(sqrt(numel(X)))

histogram 稍微调整 bin 的数目,使 bin 边界落在“合适”的数字上,而不是机械遵循这些精确的公式。

对于 datetimeduration 数据,将 bin 宽度指定为下列时间单位之一。

描述数据类型
"second"

每个 bin 是 1 秒。

datetimeduration
"minute"

每个 bin 是 1 分钟。

datetimeduration
"hour"

每个 bin 是 1 小时。

datetimeduration
"day"

每个 bin 表示 1 个日历日。此值将夏令时变化考虑在内。

datetimeduration
"week"每个 bin 是 1 个日历周。仅限 datetime
"month"每个 bin 是 1 个日历月。仅限 datetime
"quarter"每个 bin 是 1 个日历季度。仅限 datetime
"year"

每个 bin 表示 1 个日历年。此值将闰日考虑在内。

datetimeduration
"decade"每个 bin 是 1 个十年(10 个日历年)。仅限 datetime
"century"每个 bin 是 1 个世纪(100 个日历年)。仅限 datetime

  • 如果为 datetimeduration 数据指定 BinMethod,则 histogram 最多可以使用 65,536(即 216)个 bin。如果指定的 bin 持续时间需要多个 bin,则 histogram 使用与最大 bin 数对应的较大的 bin 宽度。

  • 如果指定 BinLimitsNumBinsBinEdgesBinWidth,则 BinMethod 设置为 'manual'

  • 如果用 BinWidthNumBinsBinEdges 指定 BinMethod,则 histogram 仅采用最后一个参数。

  • 此选项不适用于分类数据。

示例: histogram(X,'BinMethod','integers') 将 bin 以整数为中心放置。

类别

全部展开

类别的显示顺序,指定为 'data''ascend''descend'

  • 'data' - 使用输入数据 C 中的类别顺序。

  • 'ascend' - 显示条形高度递增的直方图。

  • 'descend' - 显示条形高度递减的直方图。

此选项仅适用于分类数据。

要显示的类别数量,指定为标量。您可以使用 'DisplayOrder' 选项更改直方图中显示类别的顺序。

此选项仅适用于分类数据。

切换未显示类别的数据汇总的显示,指定为 'on''off',或者指定为数值或逻辑值 1 (true) 或 0 (false)。值 'on' 等效于 true'off' 等效于 false。因此,您可以使用此属性的值作为逻辑值。该值存储为 matlab.lang.OnOffSwitchState 类型的 on/off 逻辑值。

  • 将此选项设置为 'on' 将在直方图中显示一个名为 'Others' 的附加条形。此附加条形用于统计不属于直方图中已显示类别的所有元素。

  • 您可以使用 'NumDisplayBins''DisplayOrder' 选项更改直方图中显示的类别数量及其顺序。

  • 此选项仅适用于分类数据。

数据

全部展开

归一化类型,指定为此表中的一个值。对于每个 bin i

  • vi 是 bin 的值。

  • ci 是 bin 中的元素数目。

  • wi 是 bin 的宽度。

  • N 是输入数据中的元素数目。此值可以大于分 bin 数据元素数,前提是数据中包含缺失值,例如 NaN,或者某些数据位于 bin 范围外。

bin 值注释
'count'(默认值)

vi=ci

  • 观测值的计数或频率。

  • 对于分类数据,bin 值之和最多为 numel(X)sum(ismember(X(:),Categories))。仅当某些输入数据不在 bin 中时,bin 值之和才会小于此值。

'probability'

vi=ciN

  • 相对概率。

  • 每个 bin 中的元素数与输入数据中的总元素数之比最大为 1。

'percentage'

vi=100*ciN

  • 相对百分比。

  • 'percentage' 不支持分类数据。

  • 每个 bin 中的元素百分比最多为 100。

'countdensity'

vi=ciwi

  • 按 bin 的宽度进行换算的计数或频率。

  • 对于分类数据,这与 'count' 相同。

  • 'countdensity' 不支持 datetimeduration 数据。

  • bin 面积的总和最多为 numel(X)

'cumcount'

vi=j=1icj

  • 累积计数,或每个 bin 及前面所有 bin 中累积的观测值数量。

  • 对于分类数据,N(end) 最多为 numel(X)sum(ismember(X(:),Categories))

'pdf'

vi=ciNwi

  • 概率密度函数的估计值。

  • 对于分类数据,这与 'probability' 相同。

  • 'pdf' 不支持 datetimeduration 数据。

  • bin 面积的总和最多为 1

'cdf'

vi=j=1icjN

  • 累积分布函数估算值。

  • 每个 bin 的计数等于该 bin 及前面所有 bin 中累积的相对观测值数量。

  • N(end) 最多为 1。

示例: histogram(X,'Normalization','pdf') 使用概率密度函数估计值对数据进行分 bin。

颜色和样式

全部展开

直方图的显示样式,指定为 'bar''stairs'

  • 'bar' - 在 A 的每个窗口上显示一个直方条形图。此方法对于减少数据中的周期性趋势很有用。

  • 'stairs' - 显示一个阶梯图,它显示直方图的轮廓而不填充内部。

示例: histogram(X,'DisplayStyle','stairs') 绘制直方图的轮廓。

条形方向,指定为 'vertical''horizontal'

示例: histogram(X,'Orientation','horizontal') 创建一个带水平条形的直方图。

分类条形的相对宽度,指定为 [0,1] 范围中的一个标量值。使用此属性可控制直方图内各分类条形的间隔。默认值是 0.9,表示条形宽度占从上一条形到下一条形之间的空间的 90%,两侧各占该空间的 5%。

如果 BarWidth1,则相邻条形彼此接触。

此选项仅适用于分类数据。

示例: 0.5

直方图的条形颜色,指定为下列值之一:

  • 'none' - 条形未填充。

  • 'auto' - 直方图条形颜色是自动选择的(默认)。

  • RGB 三元组、十六进制颜色代码或颜色名称 - 用指定的颜色填充条形。

    RGB 三元组和十六进制颜色代码对于指定自定义颜色非常有用。

    • RGB 三元组是包含三个元素的行向量,其元素分别指定颜色中红、绿、蓝分量的强度。强度值必须位于 [0,1] 范围内,例如 [0.4 0.6 0.7]

    • 十六进制颜色代码是字符向量或字符串标量,以井号 (#) 开头,后跟三个或六个十六进制数字,范围可以是 0F。这些值不区分大小写。因此,颜色代码 "#FF8800""#ff8800""#F80""#f80" 是等效的。

    此外,还可以按名称指定一些常见的颜色。下表列出了命名颜色选项、等效 RGB 三元组和十六进制颜色代码。

    颜色名称短名称RGB 三元组十六进制颜色代码外观
    "red""r"[1 0 0]"#FF0000"

    Sample of the color red

    "green""g"[0 1 0]"#00FF00"

    Sample of the color green

    "blue""b"[0 0 1]"#0000FF"

    Sample of the color blue

    "cyan" "c"[0 1 1]"#00FFFF"

    Sample of the color cyan

    "magenta""m"[1 0 1]"#FF00FF"

    Sample of the color magenta

    "yellow""y"[1 1 0]"#FFFF00"

    Sample of the color yellow

    "black""k"[0 0 0]"#000000"

    Sample of the color black

    "white""w"[1 1 1]"#FFFFFF"

    Sample of the color white

    以下是 MATLAB® 在许多类型的绘图中使用的默认颜色的 RGB 三元组和十六进制颜色代码。

    RGB 三元组十六进制颜色代码外观
    [0 0.4470 0.7410]"#0072BD"

    Sample of RGB triplet [0 0.4470 0.7410], which appears as dark blue

    [0.8500 0.3250 0.0980]"#D95319"

    Sample of RGB triplet [0.8500 0.3250 0.0980], which appears as dark orange

    [0.9290 0.6940 0.1250]"#EDB120"

    Sample of RGB triplet [0.9290 0.6940 0.1250], which appears as dark yellow

    [0.4940 0.1840 0.5560]"#7E2F8E"

    Sample of RGB triplet [0.4940 0.1840 0.5560], which appears as dark purple

    [0.4660 0.6740 0.1880]"#77AC30"

    Sample of RGB triplet [0.4660 0.6740 0.1880], which appears as medium green

    [0.3010 0.7450 0.9330]"#4DBEEE"

    Sample of RGB triplet [0.3010 0.7450 0.9330], which appears as light blue

    [0.6350 0.0780 0.1840]"#A2142F"

    Sample of RGB triplet [0.6350 0.0780 0.1840], which appears as dark red

如果将 DisplayStyle 指定为 'stairs',则 histogram 不使用 FaceColor 属性。

示例: histogram(X,'FaceColor','g') 创建一个带绿色条形的直方图。

直方图的边界颜色,指定为下列值之一:

  • 'none' - 不绘制边。

  • 'auto' - 每个边界的颜色都是自动选择的。

  • RGB 三元组、十六进制颜色代码或颜色名称 - 边使用指定的颜色。

    RGB 三元组和十六进制颜色代码对于指定自定义颜色非常有用。

    • RGB 三元组是包含三个元素的行向量,其元素分别指定颜色中红、绿、蓝分量的强度。强度值必须位于 [0,1] 范围内,例如 [0.4 0.6 0.7]

    • 十六进制颜色代码是字符向量或字符串标量,以井号 (#) 开头,后跟三个或六个十六进制数字,范围可以是 0F。这些值不区分大小写。因此,颜色代码 "#FF8800""#ff8800""#F80""#f80" 是等效的。

    此外,还可以按名称指定一些常见的颜色。下表列出了命名颜色选项、等效 RGB 三元组和十六进制颜色代码。

    颜色名称短名称RGB 三元组十六进制颜色代码外观
    "red""r"[1 0 0]"#FF0000"

    Sample of the color red

    "green""g"[0 1 0]"#00FF00"

    Sample of the color green

    "blue""b"[0 0 1]"#0000FF"

    Sample of the color blue

    "cyan" "c"[0 1 1]"#00FFFF"

    Sample of the color cyan

    "magenta""m"[1 0 1]"#FF00FF"

    Sample of the color magenta

    "yellow""y"[1 1 0]"#FFFF00"

    Sample of the color yellow

    "black""k"[0 0 0]"#000000"

    Sample of the color black

    "white""w"[1 1 1]"#FFFFFF"

    Sample of the color white

    以下是 MATLAB 在许多类型的绘图中使用的默认颜色的 RGB 三元组和十六进制颜色代码。

    RGB 三元组十六进制颜色代码外观
    [0 0.4470 0.7410]"#0072BD"

    Sample of RGB triplet [0 0.4470 0.7410], which appears as dark blue

    [0.8500 0.3250 0.0980]"#D95319"

    Sample of RGB triplet [0.8500 0.3250 0.0980], which appears as dark orange

    [0.9290 0.6940 0.1250]"#EDB120"

    Sample of RGB triplet [0.9290 0.6940 0.1250], which appears as dark yellow

    [0.4940 0.1840 0.5560]"#7E2F8E"

    Sample of RGB triplet [0.4940 0.1840 0.5560], which appears as dark purple

    [0.4660 0.6740 0.1880]"#77AC30"

    Sample of RGB triplet [0.4660 0.6740 0.1880], which appears as medium green

    [0.3010 0.7450 0.9330]"#4DBEEE"

    Sample of RGB triplet [0.3010 0.7450 0.9330], which appears as light blue

    [0.6350 0.0780 0.1840]"#A2142F"

    Sample of RGB triplet [0.6350 0.0780 0.1840], which appears as dark red

示例: histogram(X,'EdgeColor','r') 创建一个带红色条形边界的直方图。

直方图条形的透明度,指定为 [0,1] 范围内的标量值。histogram 对所有的直方图条形使用相同的透明度。值 1 表示完全不透明,0 则表示完全透明(不可见)。

示例: histogram(X,'FaceAlpha',1) 创建一个带有完全不透明条形的直方图。

直方图条形边界的透明度,指定为范围 [0,1] 内的标量值。值 1 表示完全不透明,0 则表示完全透明(不可见)。

示例: histogram(X,'EdgeAlpha',0.5) 创建一个具有半透明条形边的直方图。

线型,指定为下表中列出的选项之一。

线型描述表示的线条
"-"实线

Sample of solid line

"--"虚线

Sample of dashed line

":"点线

Sample of dotted line

"-."点划线

Sample of dash-dotted line, with alternating dashes and dots

"none"无线条无线条

条形轮廓的宽度,指定为以磅为单位的正值。一磅等于 1/72 英寸。

示例: 1.5

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

输出参量

全部展开

直方图,以对象的形式返回。有关详细信息,请参阅 Histogram 属性

属性

Histogram 属性直方图的外观和行为

对象函数

morebins增加直方图的 bin 数量
fewerbins减少直方图 bin 数量

示例

全部折叠

生成 10,000 个随机数并创建直方图。histogram 函数自动选择合适的 bin 数量,以便涵盖 x 中的值范围并显示基本分布的形状。

x = randn(10000,1);
h = histogram(x)

Figure contains an axes object. The axes object contains an object of type histogram.

h = 
  Histogram with properties:

             Data: [10000x1 double]
           Values: [2 2 1 6 7 17 29 57 86 133 193 271 331 421 540 613 730 748 776 806 824 721 623 503 446 326 234 191 132 78 65 33 26 11 8 5 5]
          NumBins: 37
         BinEdges: [-3.8000 -3.6000 -3.4000 -3.2000 -3 -2.8000 -2.6000 -2.4000 -2.2000 -2 -1.8000 -1.6000 -1.4000 -1.2000 -1 -0.8000 -0.6000 -0.4000 -0.2000 0 0.2000 0.4000 0.6000 0.8000 1.0000 1.2000 1.4000 1.6000 1.8000 2.0000 2.2000 ... ] (1x38 double)
         BinWidth: 0.2000
        BinLimits: [-3.8000 3.6000]
    Normalization: 'count'
        FaceColor: 'auto'
        EdgeColor: [0 0 0]

  Use GET to show all properties

指定 histogram 函数的输出参量时,它返回一个二元直方图对象。可以使用该对象检查直方图的属性,例如 bin 数量或宽度。

计算直方图的 bin 数量。

nbins = h.NumBins
nbins = 37

对分类为 25 个等间距 bin 的 1,000 个随机数绘制直方图。

x = randn(1000,1);
nbins = 25;
h = histogram(x,nbins)

Figure contains an axes object. The axes object contains an object of type histogram.

h = 
  Histogram with properties:

             Data: [1000x1 double]
           Values: [1 3 0 6 14 19 31 54 74 80 92 122 104 115 88 80 38 32 21 9 5 5 5 0 2]
          NumBins: 25
         BinEdges: [-3.4000 -3.1200 -2.8400 -2.5600 -2.2800 -2 -1.7200 -1.4400 -1.1600 -0.8800 -0.6000 -0.3200 -0.0400 0.2400 0.5200 0.8000 1.0800 1.3600 1.6400 1.9200 2.2000 2.4800 2.7600 3.0400 3.3200 3.6000]
         BinWidth: 0.2800
        BinLimits: [-3.4000 3.6000]
    Normalization: 'count'
        FaceColor: 'auto'
        EdgeColor: [0 0 0]

  Use GET to show all properties

求 bin 计数。

counts = h.Values
counts = 1×25

     1     3     0     6    14    19    31    54    74    80    92   122   104   115    88    80    38    32    21     9     5     5     5     0     2

生成 1,000 个随机数并创建直方图。

X = randn(1000,1);
h = histogram(X)

Figure contains an axes object. The axes object contains an object of type histogram.

h = 
  Histogram with properties:

             Data: [1000x1 double]
           Values: [3 1 2 15 17 27 53 79 85 101 127 110 124 95 67 32 27 16 6 6 4 1 2]
          NumBins: 23
         BinEdges: [-3.3000 -3.0000 -2.7000 -2.4000 -2.1000 -1.8000 -1.5000 -1.2000 -0.9000 -0.6000 -0.3000 0 0.3000 0.6000 0.9000 1.2000 1.5000 1.8000 2.1000 2.4000 2.7000 3 3.3000 3.6000]
         BinWidth: 0.3000
        BinLimits: [-3.3000 3.6000]
    Normalization: 'count'
        FaceColor: 'auto'
        EdgeColor: [0 0 0]

  Use GET to show all properties

使用 morebins 函数粗略调整 bin 数量。

Nbins = morebins(h);
Nbins = morebins(h)

Figure contains an axes object. The axes object contains an object of type histogram.

Nbins = 29

通过显式设置 bin 数按精细颗粒级别调整 bin。

h.NumBins = 31;

Figure contains an axes object. The axes object contains an object of type histogram.

生成 1,000 个随机数并创建直方图。将 bin 边界指定为向量,使宽 bin 在直方图的两边,以捕获不满足 |x|<2 的离群值。第一个向量元素是第一个 bin 的左边界,而最后一个向量元素是最后一个 bin 的右边界。

x = randn(1000,1);
edges = [-10 -2:0.25:2 10];
h = histogram(x,edges);

Figure contains an axes object. The axes object contains an object of type histogram.

Normalization 属性指定为 'countdensity' 以使包含离群值的 bin 扁平化。现在,每个 bin 的区域(而不是高度)表示该 bin 的观测值频率。

h.Normalization = 'countdensity';

Figure contains an axes object. The axes object contains an object of type histogram.

创建一个表示投票的分类向量。该向量中的类别是 'yes''no''undecided'

A = [0 0 1 1 1 0 0 0 0 NaN NaN 1 0 0 0 1 0 1 0 1 0 0 0 1 1 1 1];
C = categorical(A,[1 0 NaN],{'yes','no','undecided'})
C = 1x27 categorical
     no      no      yes      yes      yes      no      no      no      no      undecided      undecided      yes      no      no      no      yes      no      yes      no      yes      no      no      no      yes      yes      yes      yes 

使用相对条形宽度 0.5 绘制投票的分类直方图。

h = histogram(C,'BarWidth',0.5)

Figure contains an axes object. The axes object contains an object of type categoricalhistogram.

h = 
  Histogram with properties:

              Data: [no    no    yes    yes    yes    no    no    no    no    undecided    undecided    yes    no    no    no    yes    no    yes    no    yes    no    no    no    yes    yes    yes    yes]
            Values: [11 14 2]
    NumDisplayBins: 3
        Categories: {'yes'  'no'  'undecided'}
      DisplayOrder: 'data'
     Normalization: 'count'
      DisplayStyle: 'bar'
         FaceColor: 'auto'
         EdgeColor: [0 0 0]

  Use GET to show all properties

生成 1,000 个随机数并使用 'probability' 归一化创建直方图。

x = randn(1000,1);
h = histogram(x,'Normalization','probability')

Figure contains an axes object. The axes object contains an object of type histogram.

h = 
  Histogram with properties:

             Data: [1000x1 double]
           Values: [0.0030 1.0000e-03 0.0020 0.0150 0.0170 0.0270 0.0530 0.0790 0.0850 0.1010 0.1270 0.1100 0.1240 0.0950 0.0670 0.0320 0.0270 0.0160 0.0060 0.0060 0.0040 1.0000e-03 0.0020]
          NumBins: 23
         BinEdges: [-3.3000 -3.0000 -2.7000 -2.4000 -2.1000 -1.8000 -1.5000 -1.2000 -0.9000 -0.6000 -0.3000 0 0.3000 0.6000 0.9000 1.2000 1.5000 1.8000 2.1000 2.4000 2.7000 3 3.3000 3.6000]
         BinWidth: 0.3000
        BinLimits: [-3.3000 3.6000]
    Normalization: 'probability'
        FaceColor: 'auto'
        EdgeColor: [0 0 0]

  Use GET to show all properties

计算条形高度的总和。通过该归一化,每个条形的高度等于在该 bin 间隔内选择观测值的概率,并且所有条形的高度总和为 1。

S = sum(h.Values)
S = 1

生成 100000 个遵循正态分布的随机数。使用标准差 15 和均值 100。

x = 100 + 15*randn(1e5,1);

绘制这些随机数的直方图。将 y 轴的刻度和标签设置为百分比。

edges = 55:15:145;
histogram(x,edges,Normalization="percentage")
ytickformat("percentage")

Figure contains an axes object. The axes object contains an object of type histogram.

生成两个随机数向量并在同一图窗中针对每个向量绘制对应的一个直方图。

x = randn(2000,1);
y = 1 + randn(5000,1);
h1 = histogram(x);
hold on
h2 = histogram(y);

Figure contains an axes object. The axes object contains 2 objects of type histogram.

由于直方图的示例大小和 bin 宽度不同,很难将它们进行比较。对这些直方图进行归一化,这样所有的条形高度相加的结果为 1 并使用统一的 bin 宽度。

h1.Normalization = 'probability';
h1.BinWidth = 0.25;
h2.Normalization = 'probability';
h2.BinWidth = 0.25;

Figure contains an axes object. The axes object contains 2 objects of type histogram.

生成 1,000 个随机数并创建直方图。返回直方图对象以调整该直方图的属性,无需重新创建整个绘图。

x = randn(1000,1);
h = histogram(x)

h = 
  Histogram with properties:

             Data: [1000×1 double]
           Values: [3 1 2 15 17 27 53 79 85 101 127 110 124 95 67 32 27 16 6 6 4 1 2]
          NumBins: 23
         BinEdges: [-3.3000 -3.0000 -2.7000 -2.4000 -2.1000 -1.8000 -1.5000 -1.2000 -0.9000 -0.6000 -0.3000 0 0.3000 0.6000 0.9000 1.2000 1.5000 1.8000 2.1000 2.4000 2.7000 3 3.3000 3.6000]
         BinWidth: 0.3000
        BinLimits: [-3.3000 3.6000]
    Normalization: 'count'
        FaceColor: 'auto'
        EdgeColor: [0 0 0]

  Show all properties

准确指定要使用的 bin 数量。

h.NumBins = 15;

通过向量指定 bin 边界。向量中的第一个值是第一个 bin 的左边界。最后一个值是最后一个 bin 的右边界。

h.BinEdges = [-3:3];

更改直方图条形的颜色。

h.FaceColor = [0 0.5 0.5];
h.EdgeColor = 'r';

生成 5,000 个均值为 5、标准差为 2 的正态分布随机数。在 Normalization 设为 'pdf' 的情况下绘制直方图可生成概率密度函数的估计值。

x = 2*randn(5000,1) + 5;
histogram(x,'Normalization','pdf')

Figure contains an axes object. The axes object contains an object of type histogram.

在本示例中,已知正态分布数据的基本分布。但是,通过将它与已知的概率密度函数进行对比,可以使用 'pdf' 直方图确定该数据的基础概率分布。

均值为 μ、标准差为 σ 以及方差为 σ2 的正态分布的概率密度函数是:

f(x,μ,σ)=1σ2π exp[-(x-μ)22σ2].

对于均值为 5、标准差为 2 的正态分布,叠加一个概率密度函数图。

hold on
y = -5:0.1:15;
mu = 5;
sigma = 2;
f = exp(-(y-mu).^2./(2*sigma^2))./(sigma*sqrt(2*pi));
plot(y,f,'LineWidth',1.5)

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

使用 savefig 函数保存 histogram 图窗。

histogram(randn(10));
savefig('histogram.fig');
close gcf

使用 openfig 重新将直方图加载到 MATLAB®。openfig 也返回图窗 h 的句柄。

h = openfig('histogram.fig');

Figure contains an axes object. The axes object contains an object of type histogram.

使用 findobj 函数从图窗句柄中查找正确的对象句柄。这样,您可以继续处理用于生成图窗的原始直方图对象。

y = findobj(h,'type','histogram')
y = 
  Histogram with properties:

             Data: [10x10 double]
           Values: [2 17 28 32 16 3 2]
          NumBins: 7
         BinEdges: [-3 -2 -1 0 1 2 3 4]
         BinWidth: 1
        BinLimits: [-3 4]
    Normalization: 'count'
        FaceColor: 'auto'
        EdgeColor: [0 0 0]

  Use GET to show all properties

提示

  • 使用 histogram 创建的直方图在绘图编辑模式下提供上下文菜单,以允许在图窗窗口中进行交互式操作。例如,您可以使用上下文菜单以交互方式更改 bin 的数量、对齐多个直方图或更改显示顺序。

  • 当您向直方图添加数据提示时,它们会显示 bin 边界和 bin 计数。

扩展功能

版本历史记录

在 R2014b 中推出

全部展开