二元直方图
全页展开
说明
二元直方图是一种数值数据条形图,它将数据分组到二维 bin 中。创建 Histogram2
对象后,可以通过更改直方图的属性值修改它的各个方面。这对快速修改 bin 属性或更改显示特别有用。
创建对象
语法
histogram2(X,Y)
histogram2(X,Y,nbins)
histogram2(X,Y,Xedges,Yedges)
histogram2('XBinEdges',Xedges,'YBinEdges',Yedges,'BinCounts',counts)
histogram2(___,Name,Value)
histogram2(ax,___)
h = histogram2(___)
描述
示例
histogram2(X,Y)
创建 X
和 Y
的二元直方图。histogram2
函数使用自动分 bin 算法,然后返回均匀面积的 bin,这些 bin 可涵盖 X
和 Y
中的元素范围并显示分布的基本形状。histogram2
将 bin 显示为三维矩形条形,这样每个条形的高度就表示 bin 中的元素数量。
示例
histogram2(X,Y,nbins)
指定要在每个维度中使用的 bin 数量。
示例
histogram2(X,Y,Xedges,Yedges)
按照在向量中指定的每个维度的 bin 边界,将 X
和 Y
划分为若干 bin。
histogram2('XBinEdges',Xedges,'YBinEdges',Yedges,'BinCounts',counts)
绘制指定的 bin 计数,而不执行任何数据分 bin。
示例
histogram2(___,Name,Value)
使用一个或多个名称-值参量为上述任一语法指定其他参数。例如,指定 Normalization
以使用不同类型的归一化。有关属性列表,请参阅 Histogram2 属性。
histogram2(ax,___)
在指定的坐标区中而不是当前坐标区 (gca
) 中绘图。ax
可以位于上述语法中的任何输入参量组合之前。
示例
h = histogram2(___)
返回 Histogram2
对象。使用此项可检查和调整二元直方图的属性。有关属性列表,请参阅 Histogram2 属性。
输入参量
全部展开
X,Y
— 要分布到各 bin 的数据(以单独参量指定)
向量 | 矩阵 | 多维数组
要分布到各 bin 的数据,指定为向量、矩阵或多维数组的单独参量。X
和 Y
的大小必须相同。histogram2
将矩阵或多维数组数据视为单个列向量 X(:)
和 Y(:)
,并绘制单个直方图。
X
和 Y
中的对应元素指定二维数据点 [X(k),Y(k)]
的 x 和 y 坐标。X
和 Y
的数据类型可以不同,但 histogram2
会将这些输入串联成占优数据类型的 N
×2
矩阵。
histogram2
忽略所有的 NaN
值。同样,histogram2
忽略 Inf
和 -Inf
值,除非 bin 边界将 Inf
或 -Inf
显式指定为 bin 边界。虽然 NaN
、Inf
和 -Inf
值通常情况下不会绘制,但在包括所有数据元素的归一化计算(如 'probability'
)中,仍然会包含这些值。
注意
如果 X
或 Y
包含类型为 int64
或 uint64
且大于 flintmax
的整数,则建议您显式指定直方图 bin 边界。histogram2
会自动使用双精度对输入数据进行分 bin,这种方式对于大于 flintmax
的数字缺少整数精度。
数据类型: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
nbins
— 每个维度中的 bin 数量
正整数标量 | 由正整数组成的二元素向量
每个维度中的 bin 数量,指定为一个正整数标量或由正整数组成的二元素向量。
如果
nbins
是标量,则histogram2
在每个维度中使用该标量所指定的 bin 数。如果
nbins
是向量,则第一个元素给出 x 维度中的 bin 数,第二个元素给出 y 维度中的 bin 数。
如果不指定 nbins
,则 histogram2
自动基于 X
和 Y
中的值计算要使用多少个 bin。
如果用 BinMethod
或 BinWidth
指定 nbins
,则 histogram2
仅采用最后一个参数。
示例: histogram2(X,Y,20)
在每个维度中使用 20 个 bin。
示例: histogram2(X,Y,[10 20])
在 x
维度使用 10 个 bin,在 y
维度使用 20 个 bin。
Xedges
— x 维度中的 bin 边界
向量
x 维度中的 bin 边界,指定为向量。第一个元素指定 x 维度中第一个 bin 的左边界。最后一个元素指定 x 维度中的最后一个 bin 的右边界。仅最后一个 bin 包含右边界。
如果用
BinMethod
、BinWidth
或NumBins
指定Xedges
和Yedges
,histogram2
只采用 bin 边界且 bin 边界必须最后指定。如果用
XBinLimits
指定Xedges
,则histogram2
仅采用Xedges
且Xedges
必须最后指定。
Yedges
— y 维度中的 bin 边界
向量
y 维度中的 bin 边界,指定为向量。第一个元素指定 y 维度中第一个 bin 的左边界。最后一个元素指定 y 维度中的最后一个 bin 的右边界。仅最后一个 bin 包含右边界。
如果用
BinMethod
、BinWidth
或NumBins
指定Yedges
和Xedges
,histogram2
只采用 bin 边界且 bin 边界必须最后指定。如果用
YBinLimits
指定Yedges
,则histogram2
仅采用Yedges
且Yedges
必须最后指定。
counts
— bin 计数
矩阵
bin 计数,指定为矩阵。当单独执行 bin 计数计算且不希望 histogram2
执行任何数据分 bin 时,可使用此输入将 bin 计数传递给 histogram2
。
counts
必须是大小为 [length(XBinEdges)-1 length(YBinEdges)-1]
的矩阵,从而为每个 bin 指定一个 bin 计数。
示例: histogram2('XBinEdges',-1:1,'YBinEdges',-2:2,'BinCounts',[1 2 3 4; 5 6 7 8])
ax
— 坐标区对象
对象
坐标区对象。如果未指定坐标区,histogram2
函数将使用当前坐标区 (gca
)。
名称-值参数
将可选的参量对组指定为 Name1=Value1,...,NameN=ValueN
,其中 Name
是参量名称,Value
是对应的值。名称-值参量必须出现在其他参量之后,但参量对组的顺序无关紧要。
示例: histogram2(X,Y,BinWidth=[5 10])
在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name
引起来。
示例: histogram2(X,Y,'BinWidth',[5 10])
注意
此处所列的属性只是一部分。有关完整列表,请参阅 Histogram2 属性。
bin
全部展开
XBinLimits
— x 维度的 bin 范围
二元素向量
x 维度的 bin 范围,指定为二元素向量 [xbmin,xbmax]
。第一个元素指示 x 维度中的第一个 bin 边界。第二个元素指示 x 维度中的最后一个 bin 边界。
此选项只对在 bin 范围 X>=xbmin & X<=xbmax
(包含这两个边界值)内的数据分 bin。
XBinLimitsMode
— x
维度中 bin 范围的选择模式
'auto'
(默认) | 'manual'
x
维度中 bin 范围的选择模式,指定为 'auto'
或 'manual'
。默认值是 'auto'
,这样 bin 范围自动调整为沿 x 轴的数据。
如果显式指定 XBinLimits
或 XBinEdges
,则 XBinLimitsMode
自动设置为 'manual'
。在这种情况下,将 XBinLimitsMode
指定为 'auto'
可将 bin 范围重新调整为该数据。
YBinLimits
— y 维度的 bin 范围
二元素向量
y 维度的 bin 范围,指定为二元素向量 [ybmin,ybmax]
。第一个元素指示 y 维度中的第一个 bin 边界。第二个元素指示 y 维度中的最后一个 bin 边界。
此选项只对在 bin 范围 Y>=ybmin & Y<=ybmax
(包含这两个边界值)内的数据分 bin。
YBinLimitsMode
— y 维度中 bin 范围的选择模式
'auto'
(默认) | 'manual'
y 维度中 bin 范围的选择模式,指定为 'auto'
或 'manual'
。默认值为 'auto'
,即根据 y 轴上的数据自动调整 bin 范围。
如果显式指定 YBinLimits
或 YBinEdges
,则 YBinLimitsMode
自动设置为 'manual'
。在这种情况下,将 YBinLimitsMode
指定为 'auto'
可将 bin 范围重新调整为该数据。
ShowEmptyBins
— 切换空 bin 的显示
'off'
(默认) | 'on'
切换空 bin 的显示,指定为 'off'
或 'on'
。默认值为 'off'
。
示例: histogram2(X,Y,'ShowEmptyBins','on')
打开空 bin 的显示。
数据
全部展开
颜色和样式
全部展开
DisplayStyle
— 直方图的显示样式
'bar3'
(默认) | 'tile'
直方图的显示样式,指定为 'bar3'
或 'tile'
。
'bar3'
- 使用三维条形显示直方图。'tile'
- 将直方图显示为矩形块阵列,这些图块用各种颜色指示 bin 值。
示例: histogram2(X,Y,'DisplayStyle','tile')
将直方图绘制为矩形块阵列。
FaceLighting
— 直方图条形上的光照效果
'lit'
(默认) | 'flat'
| 'none'
直方图条形上的光照效果,指定为以下值之一。
值 | 描述 |
---|---|
'lit' | 直方图条形显示伪光照效果,其中条形的侧边使用相对于顶部更暗的颜色。条形不受坐标区中其他光源的影响。 这是当 |
'flat' | 直方图条形不会自动被照亮。若存在其他光源对象,则光照效果在条形面上是统一的。 |
'none' | 直方图条形不会自动被照亮,且光源不影响直方图条形。 仅当 |
示例: histogram2(X,Y,'FaceLighting','none')
关闭直方图条形的光照。
输出参量
全部展开
h
— 二元直方图
对象
二元直方图,以对象形式返回。有关详细信息,请参阅 Histogram2 属性。
属性
Histogram2 属性 | 二元直方图的外观和行为 |
对象函数
morebins | 增加直方图的 bin 数量 |
fewerbins | 减少直方图 bin 数量 |
示例
全部折叠
向量的直方图
打开实时脚本
生成 10000 个随机数对组并创建一个二元直方图。histogram2
函数自动选择合适的 bin 数量,以便涵盖 x
和 y
中的值范围并显示基本分布的形状。
x = randn(10000,1);y = randn(10000,1);h = histogram2(x,y)
h = Histogram2 with properties: Data: [10000x2 double] Values: [25x28 double] NumBins: [25 28] XBinEdges: [-3.9000 -3.6000 -3.3000 -3 -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.0000 3.3000 3.6000] YBinEdges: [-4.2000 -3.9000 -3.6000 -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 3.9000 4.2000] BinWidth: [0.3000 0.3000] Normalization: 'count' FaceColor: 'auto' EdgeColor: [0.1500 0.1500 0.1500] Use GET to show all properties
xlabel('x')ylabel('y')
指定 histogram2
函数的输出参量时,它返回一个二元直方图对象。可以使用该对象检查直方图的属性,例如 bin 数量或宽度。
计算每个维度的直方图 bin 数量。
nXnY = h.NumBins
nXnY = 1×2 25 28
指定直方图的 bin 数量
打开实时脚本
为划分入 25 个等间距 bin 的 1,000 个随机数对组绘制一个二元直方图,其中每个维度使用 5 个 bin。
x = randn(1000,1);y = randn(1000,1);nbins = 5;h = histogram2(x,y,nbins)
h = Histogram2 with properties: Data: [1000x2 double] Values: [5x5 double] NumBins: [5 5] XBinEdges: [-4 -2.4000 -0.8000 0.8000 2.4000 4] YBinEdges: [-4 -2.4000 -0.8000 0.8000 2.4000 4] BinWidth: [1.6000 1.6000] Normalization: 'count' FaceColor: 'auto' EdgeColor: [0.1500 0.1500 0.1500] Use GET to show all properties
求生成的 bin 计数。
counts = h.Values
counts = 5×5 0 2 3 1 0 2 40 124 47 4 1 119 341 109 10 1 32 117 33 1 0 4 8 1 0
调整直方图 bin 的数量
打开实时脚本
生成 1,000 个随机数对组并创建一个二元直方图。
x = randn(1000,1);y = randn(1000,1);h = histogram2(x,y)
h = Histogram2 with properties: Data: [1000x2 double] Values: [15x15 double] NumBins: [15 15] XBinEdges: [-3.5000 -3 -2.5000 -2 -1.5000 -1 -0.5000 0 0.5000 1 1.5000 2 2.5000 3 3.5000 4] YBinEdges: [-3.5000 -3 -2.5000 -2 -1.5000 -1 -0.5000 0 0.5000 1 1.5000 2 2.5000 3 3.5000 4] BinWidth: [0.5000 0.5000] Normalization: 'count' FaceColor: 'auto' EdgeColor: [0.1500 0.1500 0.1500] Use GET to show all properties
使用 morebins
函数精略调整 x 维度中的 bin 数量。
nbins = morebins(h,'x');nbins = morebins(h,'x')
nbins = 1×2 19 15
使用 fewerbins
函数调整 y 维度中的 bin 数量。
nbins = fewerbins(h,'y');nbins = fewerbins(h,'y')
nbins = 1×2 19 11
通过显式设置 bin 数按精细粒度级别调整 bin 数量。
h.NumBins = [20 10];
按高度对直方图条形着色
打开实时脚本
使用 1000 个正态分布的随机数创建一个二元直方图,其中每个维度有 12 个 bin。将 FaceColor
指定为 'flat'
以按高度对直方图条形着色。
h = histogram2(randn(1000,1),randn(1000,1),[12 12],'FaceColor','flat');colorbar
块状直方图视图
打开实时脚本
生成随机数据并绘制一个二元块状直方图。通过将 ShowEmptyBins
指定为 'on'
显示空 bin。
x = 2*randn(1000,1)+2;y = 5*randn(1000,1)+3;h = histogram2(x,y,'DisplayStyle','tile','ShowEmptyBins','on');
指定直方图的 bin 边界
打开实时脚本
生成 1,000 个随机数对组并创建一个二元直方图。使用两个向量指定 bin 边界,使具有无限宽的 bin 在直方图的边缘,以捕获不满足 的所有离群值。
x = randn(1000,1);y = randn(1000,1);Xedges = [-Inf -2:0.4:2 Inf];Yedges = [-Inf -2:0.4:2 Inf];h = histogram2(x,y,Xedges,Yedges)
h = Histogram2 with properties: Data: [1000x2 double] Values: [12x12 double] NumBins: [12 12] XBinEdges: [-Inf -2 -1.6000 -1.2000 -0.8000 -0.4000 0 0.4000 0.8000 1.2000 1.6000 2 Inf] YBinEdges: [-Inf -2 -1.6000 -1.2000 -0.8000 -0.4000 0 0.4000 0.8000 1.2000 1.6000 2 Inf] BinWidth: 'nonuniform' Normalization: 'count' FaceColor: 'auto' EdgeColor: [0.1500 0.1500 0.1500] Use GET to show all properties
当 bin 边界为无限时,histogram2
将每个离群值的 bin(沿直方图的边界)显示为其相邻 bin 宽度的两倍。
将 Normalization
属性指定为 'countdensity'
以删除包含离群值的 bin。现在,每个 bin 的体积表示该 bin 的观测值频率。
h.Normalization = 'countdensity';
归一化的直方图
打开实时脚本
生成 1000 个随机数对组并使用 'probability'
归一化创建一个二元直方图。
x = randn(1000,1);y = randn(1000,1);h = histogram2(x,y,'Normalization','probability')
h = Histogram2 with properties: Data: [1000x2 double] Values: [15x15 double] NumBins: [15 15] XBinEdges: [-3.5000 -3 -2.5000 -2 -1.5000 -1 -0.5000 0 0.5000 1 1.5000 2 2.5000 3 3.5000 4] YBinEdges: [-3.5000 -3 -2.5000 -2 -1.5000 -1 -0.5000 0 0.5000 1 1.5000 2 2.5000 3 3.5000 4] BinWidth: [0.5000 0.5000] Normalization: 'probability' FaceColor: 'auto' EdgeColor: [0.1500 0.1500 0.1500] Use GET to show all properties
计算条形高度的总和。通过此归一化,每个条形的高度等于选取位于该 bin 间隔内的观测值的概率,并且所有条形的高度总和为 1。
S = sum(h.Values(:))
S = 1
使用百分比的直方图
打开实时脚本
生成 100000 个遵循正态分布的随机向量。使用标准差 15、均值 100 和 85。
x = [100 85] + 15*randn(1e5,2);
绘制这些随机向量的直方图。对 z 轴以百分比形式缩放并加标签。
edges = 40:15:145;histogram2(x(:,1),x(:,2),edges,edges,Normalization="percentage")ztickformat("percentage")
调整直方图属性
打开实时脚本
生成 1,000 个随机数对组并创建一个二元直方图。返回直方图对象以调整该直方图的属性,无需重新创建整个绘图。
x = randn(1000,1);y = randn(1000,1);h = histogram2(x,y)
h = Histogram2 with properties: Data: [1000x2 double] Values: [15x15 double] NumBins: [15 15] XBinEdges: [-3.5000 -3 -2.5000 -2 -1.5000 -1 -0.5000 0 0.5000 1 1.5000 2 2.5000 3 3.5000 4] YBinEdges: [-3.5000 -3 -2.5000 -2 -1.5000 -1 -0.5000 0 0.5000 1 1.5000 2 2.5000 3 3.5000 4] BinWidth: [0.5000 0.5000] Normalization: 'count' FaceColor: 'auto' EdgeColor: [0.1500 0.1500 0.1500] Use GET to show all properties
按高度对直方图条形着色。
h.FaceColor = 'flat';
更改每个方向的 bin 数量。
h.NumBins = [10 25];
将直方图显示为块状图。
h.DisplayStyle = 'tile';view(2)
保存并加载二元直方图对象
打开实时脚本
使用 savefig
函数保存 histogram2
图窗。
histogram2(randn(100,1),randn(100,1));savefig('histogram2.fig');close gcf
使用 openfig
重新将直方图加载到 MATLAB®。openfig
也返回图窗 h
的句柄。
h = openfig('histogram2.fig');
使用 findobj
函数从图窗句柄中查找正确的对象句柄。这样,您可以继续处理用于生成图窗的原始直方图对象。
y = findobj(h,'type','histogram2')
y = Histogram2 with properties: Data: [100x2 double] Values: [7x6 double] NumBins: [7 6] XBinEdges: [-3 -2 -1 0 1 2 3 4] YBinEdges: [-3 -2 -1 0 1 2 3] BinWidth: [1 1] Normalization: 'count' FaceColor: 'auto' EdgeColor: [0.1500 0.1500 0.1500] Use GET to show all properties
提示
使用
histogram2
创建的直方图在绘图编辑模式下提供上下文菜单,以允许在图窗窗口中进行交互式操作。例如,您可以使用上下文菜单以交互方式更改 bin 的数量、对齐多个直方图或更改显示顺序。
扩展功能
tall 数组
对行数太多而无法放入内存的数组进行计算。
此函数支持 tall 数组,但存在以下限制:
不支持某些输入选项。允许使用的选项包括:
'BinWidth'
'XBinLimits'
'YBinLimits'
'Normalization'
'DisplayStyle'
'BinMethod'
-'auto'
和'scott'
是相同的 bin 方法。不支持'fd'
bin 方法。'EdgeAlpha'
'EdgeColor'
'FaceAlpha'
'FaceColor'
'LineStyle'
'LineWidth'
'Orientation'
此外,条形的最大数量存在上限。默认最大值为 100。
不支持
morebins
和fewerbins
方法。不支持编辑需要重新计算 bin 的直方图对象属性。
有关详细信息,请参阅 使用 tall 数组处理无法放入内存的数据。
版本历史记录
在 R2015b 中推出
全部展开
R2023b: 使用百分比进行归一化
通过将 Normalization
名称-值参量设置为 'percentage'
,可以在垂直轴上使用百分比创建直方图。
另请参阅
Histogram2 属性 | bar3 | discretize | fewerbins | morebins | histcounts2 | histcounts
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 简体中文
- English
- 日本 (日本語)
- 한국 (한국어)
Contact your local office