Matlab二元直方图绘制函数histogram2

4.3
(3)

今天,带来Matlab中绘制二元直方图,即绘制两个元素直方图的函数histogram2,它与直方图函数一致,只不过多了一个维度,它将数据分组到二维 bin 中。

本文,主要介绍histogram2函数的常见用法、语法说明、向量直方图、指定分割区块(bin)数量、修改直方图的bin数量、按高度对直方图条形着色、块状直方图视图、指定直方图bin的边界、直方图的归一化、直方图属性的调整、直方图的保存与加载等方面的介绍。

Matlab二元直方图绘制函数histogram2

下面我们将开始非常详细的 Matlab histogram2 函数语法介绍,实例引用,结果展示。首先,我们给出 Matlab 中关于 histogram2 函数的帮助文本如下:

>> help histogram2
 histogram2  Plots a bivariate histogram.
    histogram2(X,Y) plots a bivariate histogram of X and Y. X and Y can be 
    arrays of any shape, but they must have the same size. histogram2 
    determines the bin edges using an automatic binning algorithm that 
    returns uniform bins of an area chosen to cover the range of values in 
    X and Y and reveal the shape of the underlying distribution. 
 
    histogram2(X,Y,NBINS), where NBINS is a scalar or 2-element vector, 
    specifies the number of bins to use. A scalar specifies the same number 
    of bins in each dimension, whereas the 2-element vector [nbinsx nbinsy] 
    specifies a different number of bins for the X and Y dimensions.
 
    histogram2(X,Y,XEDGES,YEDGES), where XEDGES and YEDGES are vectors, 
    specifies the edges of the bins.
 
    The value [X(k),Y(k)] is in the (i,j)th bin if XEDGES(i) <= X(k) < 
    XEDGES(i+1) and YEDGES(j) <= Y(k) < YEDGES(j+1). The last bins in the
    X and Y dimensions will also include the upper edge. For example, 
    [X(k),Y(k)] will fall into the i-th bin in the last row if 
    XEDGES(end-1) <= X(k) <= XEDGES(end) && YEDGES(i) <= Y(k) < YEDGES(i+1).
 
    histogram2(...,'BinWidth',BW) where BW is a scalar or 2-element vector, 
    uses bins of size BW. A scalar specifies the same bin width for each 
    dimension, whereas the 2-element vector [bwx bwy] specifies different 
    bin widths for the X and Y dimensions. To prevent from accidentally 
    creating too many bins, a limit of 1024 bins can be created along each 
    dimension when specifying 'BinWidth'. If BW is too small such that more 
    than 1024 bins are needed in either dimension, histogram2 uses larger 
    bins instead.
 
    histogram2(...,'XBinLimits',[XBMIN,XBMAX]) plots a histogram 
    with only elements between the bin limits inclusive along the X axis, 
    X>=BMINX & X<=BMAXX.  Similarly, 
    histogram2(...,'YBinLimits',[YBMIN,YBMAX]) uses only elements between 
    the bin limits inclusive along the Y axis, Y>=YBMIN & Y<=YBMAX.
 
    histogram2(...,'Normalization',NM) specifies the normalization scheme 
    of the histogram values. The normalization scheme affects the scaling 
    of the histogram along the Z axis. NM can be:
                   'count'   The height of each bar is the number of 
                             observations in each bin, and the sum of the
                             bar heights is NUMEL(X) or NUMEL(Y).
             'probability'   The height of each bar is the relative 
                             number of observations (number of observations
                             in bin / total number of observations), and
                             the sum of the bar heights is 1.
            'countdensity'   The height of each bar is, (the number of 
                             observations in each bin) / (area of bin). The 
                             volume (height * area) of each bar is the number
                             of observations in the bin, and the sum of
                             the bar volumes is NUMEL(X) or NUMEL(Y).
                     'pdf'   Probability density function estimate. The height 
                             of each bar is, (number of observations in bin)
                             / (total number of observations * area of bin).
                             The volume of each bar is the relative number of
                             observations, and the sum of the bar volumes 
                             is 1.
                'cumcount'   The height of each bar is the cumulative 
                             number of observations in each bin and all
                             previous bins in both the X and Y dimensions. 
                             The height of the last bar is NUMEL(X) or 
                             NUMEL(Y).
                     'cdf'   Cumulative density function estimate. The height 
                             of each bar is the cumulative relative number
                             of observations in each bin and all previous 
                             bins in both the X and Y dimensions. The height 
                             of the last bar is 1.
 
    histogram2(...,'DisplayStyle',STYLE) specifies the display style of the 
    histogram. STYLE can be:
                    'bar3'   Display histogram using 3-D bars. This is the 
                             default.
                    'tile'   Display histogram as a rectangular array of 
                             tiles with colors indicating the bin values.
 
    histogram2(...,'BinMethod',BM), uses the specified automatic binning 
    algorithm to determine the number and width of the bins.  BM can be:
                    'auto'   The default 'auto' algorithm chooses a bin 
                             size to cover the data range and reveal the 
                             shape of the underlying distribution.
                   'scott'   Scott's rule is optimal if X and Y are close 
                             to being jointly normally distributed, but 
                             is also appropriate for most other 
                             distributions. It uses a bin size of 
                             [3.5*STD(X(:))*NUMEL(X)^(-1/4)
                             3.5*STD(Y(:))*NUMEL(Y)^(-1/4)]
                      'fd'   The Freedman-Diaconis rule is less sensitive to 
                             outliers in the data, and may be more suitable 
                             for data with heavy-tailed distributions. It 
                             uses a bin size of [2*IQR(X(:))*NUMEL(X)^(-1/4) 
                             2*IQR(Y(:))*NUMEL(Y)^(-1/4)] where IQR is the 
                             interquartile range.
                'integers'   The integer rule is useful with integer data, 
                             as it creates a bin for each pair of integer 
                             X and Y. It uses a bin width of 1 along each 
                             dimension and places bin edges halfway 
                             between integers. To prevent from accidentally 
                             creating too many bins, a limit of 1024 bins 
                             can be created along each dimension with this 
                             rule. If the data range along either dimension 
                             is greater than 1024, then larger bins are 
                             used instead.
 
    histogram2(...,NAME,VALUE) set the property NAME to VALUE. 
      
    histogram2(AX,...) plots into AX instead of the current axes.
        
    H = histogram2(...) also returns a Histogram2 object. Use this to 
    inspect and adjust the properties of the histogram.
 
    Class support for inputs X, Y, XEDGES, YEDGES:
       float: double, single
       integers: uint8, int8, uint16, int16, uint32, int32, uint64, int64
       logical

常见用法

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) 使用向量 Xedges 和 Yedges 指定每个维度中 bin 的边界。

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

histogram2(___,Name,Value) 使用前面的任何语法指定具有一个或多个 Name,Value 对组参数的其他选项。例如,可以指定 ‘BinWidth’ 和一个二元素向量以调整每个维度中 bin 的宽度,或指定 ‘Normalization’ 和一个有效选项(’count’、’probability’、’countdensity’、’pdf’、’cumcount’ 或 ‘cdf’)以使用不同类型的归一化。

histogram2(ax,___) 将图形绘制到 ax 指定的坐标区中,而不是当前坐标区 (gca) 中。选项 ax 可以位于前面的语法中的任何输入参数组合之前。

h = histogram2(___) 返回 Histogram2 对象。使用此项可检查和调整二元直方图的属性。

向量的直方图

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

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

输出结果如下:

h = 

  Histogram2 (具有属性):

             Data: [10000x2 double]
           Values: [25x28 double]
          NumBins: [25 28]
        XBinEdges: [1x26 double]
        YBinEdges: [1x29 double]
         BinWidth: [0.3000 0.3000]
    Normalization: 'count'
        FaceColor: 'auto'
        EdgeColor: [0.1500 0.1500 0.1500]
xlabel('x')
ylabel('y')
Matlab二元直方图绘制函数histogram2

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

计算每个维度的直方图 bin 数量。

nXnY = h.NumBins

输出结果如下:

nXnY =

    26    27

指定直方图的 bin 数量

为划分入 25 个等距 bin 的 1,000 个随机数对组绘制一个二元直方图,其中每个维度使用 5 个 bin。

x = randn(1000,1);
y = randn(1000,1);
nbins = 5;
h = histogram2(x,y,nbins)
Matlab二元直方图绘制函数histogram2

输出结果如下:

h = 

  Histogram2 (具有属性):

             Data: [1000x2 double]
           Values: [5x5 double]
          NumBins: [5 5]
        XBinEdges: [-5 -3.4000 -1.8000 -0.2000 1.4000 3]
        YBinEdges: [-4 -2.5000 -1 0.5000 2 3.5000]
         BinWidth: [1.6000 1.5000]
    Normalization: 'count'
        FaceColor: 'auto'
        EdgeColor: [0.1500 0.1500 0.1500]

求生成的 bin 计数。

counts = h.Values

输出结果如下:

counts =

     0     0     1     1     0
     0     5    20    13     0
     3    64   211   113    11
     2    73   245   142    17
     1    10    43    24     1

调整直方图 bin 的数量

生成 1,000 个随机数对组并创建一个二元直方图。

x = randn(1000,1);
y = randn(1000,1);
h = histogram2(x,y)
Matlab二元直方图绘制函数histogram2

输出结果如下:

h = 

  Histogram2 (具有属性):

             Data: [1000x2 double]
           Values: [16x13 double]
          NumBins: [16 13]
        XBinEdges: [-4 -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]
         BinWidth: [0.5000 0.5000]
    Normalization: 'count'
        FaceColor: 'auto'
        EdgeColor: [0.1500 0.1500 0.1500]

使用 morebins 函数精略调整 x 维度中的 bin 数量。

nbins = morebins(h,'x');
nbins = morebins(h,'x')
Matlab二元直方图绘制函数histogram2

输出结果如下:

nbins =

    20    13

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

nbins = fewerbins(h,'y');
nbins = fewerbins(h,'y')
Matlab二元直方图绘制函数histogram2

输出结果如下:

nbins =

    20     9

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

h.NumBins = [20 10];
Matlab二元直方图绘制函数histogram2

按高度对直方图条形着色

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

h = histogram2(randn(1000,1),randn(1000,1),[12 12],'FaceColor','flat');
colorbar
Matlab二元直方图绘制函数histogram2

块状直方图视图

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

x = 2*randn(1000,1)+2;
y = 5*randn(1000,1)+3;
h = histogram2(x,y,'DisplayStyle','tile','ShowEmptyBins','on');
Matlab二元直方图绘制函数histogram2

指定直方图的 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二元直方图绘制函数histogram2

输出结果如下:

h = 

  Histogram2 (具有属性):

             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]

当 bin 边界为无限时,histogram2 将每个离群值的 bin(沿直方图的边界)显示为其相邻 bin 宽度的两倍。

将 Normalization 属性指定为 ‘countdensity’ 以删除包含离群值的 bin。现在,每个 bin 的体积表示该 bin 的观测值频率。

h.Normalization = 'countdensity';
Matlab二元直方图绘制函数histogram2

归一化的直方图

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

x = randn(1000,1);
y = randn(1000,1);
h = histogram2(x,y,'Normalization','probability')
Matlab二元直方图绘制函数histogram2

输出结果如下:

h = 

  Histogram2 (具有属性):

             Data: [1000x2 double]
           Values: [14x13 double]
          NumBins: [14 13]
        XBinEdges: [-3.5000 -3 -2.5000 -2 -1.5000 -1 -0.5000 0 0.5000 1 1.5000 2 2.5000 3 3.5000]
        YBinEdges: [-3 -2.5000 -2 -1.5000 -1 -0.5000 0 0.5000 1 1.5000 2 2.5000 3 3.5000]
         BinWidth: [0.5000 0.5000]
    Normalization: 'probability'
        FaceColor: 'auto'
        EdgeColor: [0.1500 0.1500 0.1500]

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

S = sum(h.Values(:))

输出结果如下:

S =

    1.0000

调整直方图属性

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

x = randn(1000,1);
y = randn(1000,1);
h = histogram2(x,y)
Matlab二元直方图绘制函数histogram2

输出结果如下:

h = 

  Histogram2 (具有属性):

             Data: [1000x2 double]
           Values: [14x14 double]
          NumBins: [14 14]
        XBinEdges: [-3.5000 -3 -2.5000 -2 -1.5000 -1 -0.5000 0 0.5000 1 1.5000 2 2.5000 3 3.5000]
        YBinEdges: [-3.5000 -3 -2.5000 -2 -1.5000 -1 -0.5000 0 0.5000 1 1.5000 2 2.5000 3 3.5000]
         BinWidth: [0.5000 0.5000]
    Normalization: 'count'
        FaceColor: 'auto'
        EdgeColor: [0.1500 0.1500 0.1500]

按高度对直方图条形着色。

h.FaceColor = 'flat';
Matlab二元直方图绘制函数histogram2

更改每个方向的 bin 数量。

h.NumBins = [10 25];
Matlab二元直方图绘制函数histogram2

将直方图显示为块状图。

h.DisplayStyle = 'tile';
view(2)
Matlab二元直方图绘制函数histogram2

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

使用 savefig 函数保存二元直方图图窗。

y = histogram2(randn(100,1),randn(100,1));
savefig('histogram2.fig');
Matlab二元直方图绘制函数histogram2
clear all
close all

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

h = openfig('histogram2.fig');

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

y = findobj(h, 'type', 'histogram2')

输出结果如下:

y = 

  Histogram2 (具有属性):

             Data: [100x2 double]
           Values: [6x6 double]
          NumBins: [6 6]
        XBinEdges: [-3 -2 -1 0 1 2 3]
        YBinEdges: [-3 -2 -1 0 1 2 3]
         BinWidth: [1 1]
    Normalization: 'count'
        FaceColor: 'auto'
        EdgeColor: [0.1500 0.1500 0.1500]

共计3人评分,平均4.3

到目前为止还没有投票~

很抱歉,这篇文章对您没有用!

让我们改善这篇文章!

告诉我们我们如何改善这篇文章?

本文来自转载,原文出处:MathWorks官网,由古哥整理发布

如若转载,请注明出处:https://iymark.com/program/matlab-data-function-histogram2.html

发表评论

登录后才能评论
分享本页
返回顶部