二元直方图 - MATLAB - MathWorks 中国 (2024)

二元直方图

全页展开

  • 二元直方图 - MATLAB- MathWorks 中国 (1)

二元直方图 - MATLAB- MathWorks 中国 (2)

说明

二元直方图是一种数值数据条形图,它将数据分组到二维 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) 创建 XY 的二元直方图。histogram2 函数使用自动分 bin 算法,然后返回均匀面积的 bin,这些 bin 可涵盖 XY 中的元素范围并显示分布的基本形状。histogram2 将 bin 显示为三维矩形条形,这样每个条形的高度就表示 bin 中的元素数量。

示例

histogram2(X,Y,nbins) 指定要在每个维度中使用的 bin 数量。

示例

histogram2(X,Y,Xedges,Yedges) 按照在向量中指定的每个维度的 bin 边界,将 XY 划分为若干 bin。

histogram2('XBinEdges',Xedges,'YBinEdges',Yedges,'BinCounts',counts) 绘制指定的 bin 计数,而不执行任何数据分 bin。

示例

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

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

示例

h = histogram2(___) 返回 Histogram2 对象。使用此项可检查和调整二元直方图的属性。有关属性列表,请参阅 Histogram2 属性

输入参量

全部展开

要分布到各 bin 的数据,指定为向量、矩阵或多维数组的单独参量。XY 的大小必须相同。histogram2 将矩阵或多维数组数据视为单个列向量 X(:)Y(:),并绘制单个直方图。

XY 中的对应元素指定二维数据点 [X(k),Y(k)] 的 x 和 y 坐标。XY 的数据类型可以不同,但 histogram2 会将这些输入串联成占优数据类型的 N×2 矩阵。

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

注意

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

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

每个维度中的 bin 数量,指定为一个正整数标量或由正整数组成的二元素向量。

  • 如果 nbins 是标量,则 histogram2 在每个维度中使用该标量所指定的 bin 数。

  • 如果 nbins 是向量,则第一个元素给出 x 维度中的 bin 数,第二个元素给出 y 维度中的 bin 数。

如果不指定 nbins,则 histogram2 自动基于 XY 中的值计算要使用多少个 bin。

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

示例: histogram2(X,Y,20) 在每个维度中使用 20 个 bin。

示例: histogram2(X,Y,[10 20])x 维度使用 10 个 bin,在 y 维度使用 20 个 bin。

x 维度中的 bin 边界,指定为向量。第一个元素指定 x 维度中第一个 bin 的左边界。最后一个元素指定 x 维度中的最后一个 bin 的右边界。仅最后一个 bin 包含右边界。

  • 如果用 BinMethodBinWidthNumBins 指定 XedgesYedgeshistogram2 只采用 bin 边界且 bin 边界必须最后指定。

  • 如果用 XBinLimits 指定 Xedges,则 histogram2 仅采用 XedgesXedges 必须最后指定。

y 维度中的 bin 边界,指定为向量。第一个元素指定 y 维度中第一个 bin 的左边界。最后一个元素指定 y 维度中的最后一个 bin 的右边界。仅最后一个 bin 包含右边界。

  • 如果用 BinMethodBinWidthNumBins 指定 YedgesXedgeshistogram2 只采用 bin 边界且 bin 边界必须最后指定。

  • 如果用 YBinLimits 指定 Yedges,则 histogram2 仅采用 YedgesYedges 必须最后指定。

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

坐标区对象。如果未指定坐标区,histogram2 函数将使用当前坐标区 (gca)。

名称-值参数

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

示例: histogram2(X,Y,BinWidth=[5 10])

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

示例: histogram2(X,Y,'BinWidth',[5 10])

注意

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

bin

全部展开

x 维度的 bin 范围,指定为二元素向量 [xbmin,xbmax]。第一个元素指示 x 维度中的第一个 bin 边界。第二个元素指示 x 维度中的最后一个 bin 边界。

此选项只对在 bin 范围 X>=xbmin & X<=xbmax(包含这两个边界值)内的数据分 bin。

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

如果显式指定 XBinLimitsXBinEdges,则 XBinLimitsMode 自动设置为 'manual'。在这种情况下,将 XBinLimitsMode 指定为 'auto' 可将 bin 范围重新调整为该数据。

y 维度的 bin 范围,指定为二元素向量 [ybmin,ybmax]。第一个元素指示 y 维度中的第一个 bin 边界。第二个元素指示 y 维度中的最后一个 bin 边界。

此选项只对在 bin 范围 Y>=ybmin & Y<=ybmax(包含这两个边界值)内的数据分 bin。

y 维度中 bin 范围的选择模式,指定为 'auto''manual'。默认值为 'auto',即根据 y 轴上的数据自动调整 bin 范围。

如果显式指定 YBinLimitsYBinEdges,则 YBinLimitsMode 自动设置为 'manual'。在这种情况下,将 YBinLimitsMode 指定为 'auto' 可将 bin 范围重新调整为该数据。

切换空 bin 的显示,指定为 'off''on'。默认值为 'off'

示例: histogram2(X,Y,'ShowEmptyBins','on') 打开空 bin 的显示。

数据

全部展开

颜色和样式

全部展开

直方图的显示样式,指定为 'bar3''tile'

  • 'bar3' - 使用三维条形显示直方图。

  • 'tile' - 将直方图显示为矩形块阵列,这些图块用各种颜色指示 bin 值。

示例: histogram2(X,Y,'DisplayStyle','tile') 将直方图绘制为矩形块阵列。

直方图条形上的光照效果,指定为以下值之一。

描述
'lit'

直方图条形显示伪光照效果,其中条形的侧边使用相对于顶部更暗的颜色。条形不受坐标区中其他光源的影响。

这是当 DisplayStyle'bar3' 时的默认值。

'flat'

直方图条形不会自动被照亮。若存在其他光源对象,则光照效果在条形面上是统一的。

'none'

直方图条形不会自动被照亮,且光源不影响直方图条形。

仅当 DisplayStyle'tile' 时,FaceLighting 才为 'none'

示例: histogram2(X,Y,'FaceLighting','none') 关闭直方图条形的光照。

输出参量

全部展开

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

属性

Histogram2 属性二元直方图的外观和行为

对象函数

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

示例

全部折叠

向量的直方图

打开实时脚本

生成 10000 个随机数对组并创建一个二元直方图。histogram2 函数自动选择合适的 bin 数量,以便涵盖 xy 中的值范围并显示基本分布的形状。

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

二元直方图 - MATLAB- MathWorks 中国 (3)

指定 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)

二元直方图 - MATLAB- MathWorks 中国 (4)

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)

二元直方图 - MATLAB- MathWorks 中国 (5)

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

二元直方图 - MATLAB- MathWorks 中国 (6)

nbins = 1×2 19 15

使用 fewerbins 函数调整 y 维度中的 bin 数量。

nbins = fewerbins(h,'y');nbins = fewerbins(h,'y')

二元直方图 - MATLAB- MathWorks 中国 (7)

nbins = 1×2 19 11

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

h.NumBins = [20 10];

二元直方图 - MATLAB- MathWorks 中国 (8)

按高度对直方图条形着色

打开实时脚本

使用 1000 个正态分布的随机数创建一个二元直方图,其中每个维度有 12 个 bin。将 FaceColor 指定为 'flat' 以按高度对直方图条形着色。

h = histogram2(randn(1000,1),randn(1000,1),[12 12],'FaceColor','flat');colorbar

二元直方图 - MATLAB- MathWorks 中国 (9)

块状直方图视图

打开实时脚本

生成随机数据并绘制一个二元块状直方图。通过将 ShowEmptyBins 指定为 'on' 显示空 bin。

x = 2*randn(1000,1)+2;y = 5*randn(1000,1)+3;h = histogram2(x,y,'DisplayStyle','tile','ShowEmptyBins','on');

二元直方图 - MATLAB- MathWorks 中国 (10)

指定直方图的 bin 边界

打开实时脚本

生成 1,000 个随机数对组并创建一个二元直方图。使用两个向量指定 bin 边界,使具有无限宽的 bin 在直方图的边缘,以捕获不满足 |x|<2 的所有离群值。

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)

二元直方图 - MATLAB- MathWorks 中国 (11)

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

二元直方图 - MATLAB- MathWorks 中国 (12)

归一化的直方图

打开实时脚本

生成 1000 个随机数对组并使用 'probability' 归一化创建一个二元直方图。

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

二元直方图 - MATLAB- MathWorks 中国 (13)

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

二元直方图 - MATLAB- MathWorks 中国 (14)

调整直方图属性

打开实时脚本

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

x = randn(1000,1);y = randn(1000,1);h = histogram2(x,y)

二元直方图 - MATLAB- MathWorks 中国 (15)

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

二元直方图 - MATLAB- MathWorks 中国 (16)

更改每个方向的 bin 数量。

h.NumBins = [10 25];

二元直方图 - MATLAB- MathWorks 中国 (17)

将直方图显示为块状图。

h.DisplayStyle = 'tile';view(2)

二元直方图 - MATLAB- MathWorks 中国 (18)

保存并加载二元直方图对象

打开实时脚本

使用 savefig 函数保存 histogram2 图窗。

histogram2(randn(100,1),randn(100,1));savefig('histogram2.fig');close gcf

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

h = openfig('histogram2.fig');

二元直方图 - MATLAB- MathWorks 中国 (19)

使用 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 的数量、对齐多个直方图或更改显示顺序。

扩展功能

版本历史记录

在 R2015b 中推出

全部展开

通过将 Normalization 名称-值参量设置为 'percentage',可以在垂直轴上使用百分比创建直方图。

另请参阅

Histogram2 属性 | bar3 | discretize | fewerbins | morebins | histcounts2 | histcounts

MATLAB 命令

您点击的链接对应于以下 MATLAB 命令:

 

请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。

二元直方图 - MATLAB- MathWorks 中国 (20)

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

Europe

Asia Pacific

Contact your local office

二元直方图 - MATLAB
- MathWorks 中国 (2024)
Top Articles
Update: 'Dangerous' man in fatal wreck before home invasion; ankle monitor stopped working
Buncombe's unaffiliated candidates face off against Democratic commissioners at CIBO forum
Jail Inquiry | Polk County Sheriff's Office
Edina Omni Portal
Warren Ohio Craigslist
Bashas Elearning
Pieology Nutrition Calculator Mobile
Access-A-Ride – ACCESS NYC
Visitor Information | Medical Center
Blanchard St Denis Funeral Home Obituaries
Craigslist Cars And Trucks For Sale By Owner Indianapolis
South Park Season 26 Kisscartoon
Z-Track Injection | Definition and Patient Education
Konkurrenz für Kioske: 7-Eleven will Minisupermärkte in Deutschland etablieren
360 Training Alcohol Final Exam Answers
Craigslist In Fredericksburg
Walgreens On Nacogdoches And O'connor
Ap Chem Unit 8 Progress Check Mcq
MindWare : Customer Reviews : Hocus Pocus Magic Show Kit
Bestellung Ahrefs
ᐅ Bosch Aero Twin A 863 S Scheibenwischer
Ostateillustrated Com Message Boards
"Une héroïne" : les funérailles de Rebecca Cheptegei, athlète olympique immolée par son compagnon | TF1 INFO
Yakimacraigslist
Invert Clipping Mask Illustrator
Saatva Memory Foam Hybrid mattress review 2024
Carson Municipal Code
Testberichte zu E-Bikes & Fahrrädern von PROPHETE.
Dwc Qme Database
Dragonvale Valor Dragon
The Boogeyman (Film, 2023) - MovieMeter.nl
Top 20 scariest Roblox games
Darktide Terrifying Barrage
WOODSTOCK CELEBRATES 50 YEARS WITH COMPREHENSIVE 38-CD DELUXE BOXED SET | Rhino
Persona 4 Golden Taotie Fusion Calculator
Los Amigos Taquería Kalona Menu
RUB MASSAGE AUSTIN
Peter Vigilante Biography, Net Worth, Age, Height, Family, Girlfriend
Autozone Locations Near Me
Craigs List Jonesboro Ar
Felix Mallard Lpsg
Nu Carnival Scenes
What is a lifetime maximum benefit? | healthinsurance.org
Beds From Rent-A-Center
Mejores páginas para ver deportes gratis y online - VidaBytes
Urban Airship Acquires Accengage, Extending Its Worldwide Leadership With Unmatched Presence Across Europe
2121 Gateway Point
Where To Find Mega Ring In Pokemon Radical Red
Skybird_06
Dumb Money Showtimes Near Regal Stonecrest At Piper Glen
Comenity/Banter
Dinargurus
Latest Posts
Article information

Author: Arielle Torp

Last Updated:

Views: 5547

Rating: 4 / 5 (41 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Arielle Torp

Birthday: 1997-09-20

Address: 87313 Erdman Vista, North Dustinborough, WA 37563

Phone: +97216742823598

Job: Central Technology Officer

Hobby: Taekwondo, Macrame, Foreign language learning, Kite flying, Cooking, Skiing, Computer programming

Introduction: My name is Arielle Torp, I am a comfortable, kind, zealous, lovely, jolly, colorful, adventurous person who loves writing and wants to share my knowledge and understanding with you.