MATLAB直方圖函數-histogram

histogram:直方圖繪制案例
1.向量直方圖
生成 10,000 個隨機數并創建直方圖。histogram 函數自動選擇合適的 bin 數量,以便涵蓋 x 中的值范圍并顯示基本分布的形狀。

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

MATLAB直方圖函數-histogram的圖1h = 

  Histogram with properties:

             Data: [10000x1 double]

           Values: [1x37 double]

          NumBins: 37

         BinEdges: [1x38 double]

         BinWidth: 0.2000

        BinLimits: [-3.8000 3.6000]

    Normalization: 'count'

        FaceColor: 'auto'

        EdgeColor: [0 0 0]

  Show all properties

指定 histogram 函數的輸出參數時,它返回一個二元直方圖對象。可以使用該對象檢查直方圖的屬性,例如 bin 數量或寬度。

計算直方圖的 bin 數量。

nbins = h.NumBins

nbins = 37

2.指定直方圖的 bin 數量

對分類為 25 個等距 bin 的 1,000 個隨機數繪制直方圖。

x = randn(1000,1);

nbins = 25;

h = histogram(x,nbins)

MATLAB直方圖函數-histogram的圖2

h = 

  Histogram with properties:

             Data: [1000x1 double]

           Values: [1x25 double]

          NumBins: 25

         BinEdges: [1x26 double]

         BinWidth: 0.2800

        BinLimits: [-3.4000 3.6000]

    Normalization: 'count'

        FaceColor: 'auto'

        EdgeColor: [0 0 0]

  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

3.更改直方圖的 bin 數量

生成 1,000 個隨機數并創建直方圖。

X = randn(1000,1);

h = histogram(X)

MATLAB直方圖函數-histogram的圖3

h = 

  Histogram with properties:

             Data: [1000x1 double]

           Values: [1x23 double]

          NumBins: 23

         BinEdges: [1x24 double]

         BinWidth: 0.3000

        BinLimits: [-3.3000 3.6000]

    Normalization: 'count'

        FaceColor: 'auto'

        EdgeColor: [0 0 0]

  Show all properties

使用 morebins 函數粗略調整 bin 數量。

Nbins = morebins(h);

Nbins = morebins(h)

MATLAB直方圖函數-histogram的圖4

Nbins = 29

通過顯式設置 bin 數按精細顆粒級別調整 bin。

h.NumBins = 31;

MATLAB直方圖函數-histogram的圖5

4.指定直方圖的 bin 邊界

生成 1,000 個隨機數并創建直方圖。將 bin 邊界指定為向量,使寬 bin 在直方圖的兩邊,以捕獲不滿足 |x|<2 的離群值。第一個向量元素是第一個 bin 的左邊界,而最后一個向量元素是最后一個 bin 的右邊界。

x = randn(1000,1);

edges = [-10 -2:0.25:2 10];

h = histogram(x,edges);

MATLAB直方圖函數-histogram的圖6

將 Normalization 屬性指定為 'countdensity' 以使包含離群值的 bin 扁平化。現在,每個 bin 的區域(而不是高度)表示該 bin 的觀測值頻率。

h.Normalization = 'countdensity';

MATLAB直方圖函數-histogram的圖7

5.繪制分類直方圖

創建一個表示投票的分類向量。該向量中的類別是 '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

  Columns 1 through 9

     no      no      yes      yes      yes      no      no      no      no 

  Columns 10 through 16

     undecided      undecided      yes      no      no      no      yes 

  Columns 17 through 25

     no      yes      no      yes      no      no      no      yes      yes 

  Columns 26 through 27

     yes      yes 

使用相對條形寬度 0.5 繪制投票的分類直方圖。

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

MATLAB直方圖函數-histogram的圖8

h = 

  Histogram with properties:

              Data: [1x27 categorical]

            Values: [11 14 2]

    NumDisplayBins: 3

        Categories: {'yes'  'no'  'undecided'}

      DisplayOrder: 'data'

     Normalization: 'count'

      DisplayStyle: 'bar'

         FaceColor: 'auto'

         EdgeColor: [0 0 0]

  Show all properties

6.具有指定歸一化的直方圖

生成 1,000 個隨機數并使用 'probability' 歸一化創建直方圖。

x = randn(1000,1);

h = histogram(x,'Normalization','probability')

MATLAB直方圖函數-histogram的圖9

h = 

  Histogram with properties:

             Data: [1000x1 double]

           Values: [1x23 double]

          NumBins: 23

         BinEdges: [1x24 double]

         BinWidth: 0.3000

        BinLimits: [-3.3000 3.6000]

    Normalization: 'probability'

        FaceColor: 'auto'

        EdgeColor: [0 0 0]

  Show all properties

計算條形高度的總和。通過該歸一化,每個條形的高度等于在該 bin 間隔內選擇觀測值的概率,并且所有條形的高度總和為 1。

S = sum(h.Values)

S = 1

7.繪制多個直方圖

生成兩個隨機數向量并在同一圖窗中針對每個向量繪制對應的一個直方圖。

x = randn(2000,1);

y = 1 + randn(5000,1);

h1 = histogram(x);

hold on

h2 = histogram(y);

MATLAB直方圖函數-histogram的圖10

由于直方圖的示例大小和 bin 寬度不同,很難將它們進行比較。對這些直方圖進行歸一化,這樣所有的條形高度相加的結果為 1 并使用統一的 bin 寬度。

h1.Normalization = 'probability';

h1.BinWidth = 0.25;

h2.Normalization = 'probability';

h2.BinWidth = 0.25;

MATLAB直方圖函數-histogram的圖11

8.調整直方圖屬性

生成 1,000 個隨機數并創建直方圖。返回直方圖對象以調整該直方圖的屬性,無需重新創建整個繪圖。

x = randn(1000,1);

h = histogram(x)

MATLAB直方圖函數-histogram的圖12

h = 

  Histogram with properties:

             Data: [1000x1 double]

           Values: [1x23 double]

          NumBins: 23

         BinEdges: [1x24 double]

         BinWidth: 0.3000

        BinLimits: [-3.3000 3.6000]

    Normalization: 'count'

        FaceColor: 'auto'

        EdgeColor: [0 0 0]

  Show all properties

準確指定要使用的 bin 數量。

h.NumBins = 15;

MATLAB直方圖函數-histogram的圖13通過向量指定 bin 邊界。向量中的第一個值是第一個 bin 的左邊界。最后一個值是最后一個 bin 的右邊界。

h.BinEdges = [-3:3];

MATLAB直方圖函數-histogram的圖14

更改直方圖條形的顏色。

h.FaceColor = [0 0.5 0.5];

h.EdgeColor = 'r';

MATLAB直方圖函數-histogram的圖15

9.確定基本概率分布

生成 5,000 個均值為 5、標準差為 2 的正態分布隨機數。在 Normalization 設為 'pdf' 的情況下繪制直方圖可生成概率密度函數的估計值。

x = 2*randn(5000,1) + 5;

histogram(x,'Normalization','pdf')

MATLAB直方圖函數-histogram的圖16

在本示例中,已知正態分布數據的基本分布。但是,通過將它與已知的概率密度函數進行對比,可以使用 'pdf' 直方圖確定該數據的基礎概率分布。

均值為 μ、標準差為 σ 以及方差為 σ^2

 的正態分布的概率密度函數是:

f(x,μ,σ)=1/(σ*sqrt(2π))*exp[?(x?μ)^2/(2*σ^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)

MATLAB直方圖函數-histogram的圖17

10.保存并加載直方圖對象

使用 savefig 函數保存直方圖。

y = histogram(randn(10));

savefig('histogram.fig');

clear all

close all

使用 openfig 重新將直方圖加載到 MATLAB。openfig 也返回圖窗 h 的句柄。

h = openfig('histogram.fig');

MATLAB直方圖函數-histogram的圖18

使用 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]

  Show all properties

【免責聲明】本文檔部分內容摘自網絡平臺,版權歸原作者所有,僅用于技術分享與交流,非商業用途!若有涉及版權等請告知,將及時修訂刪除,謝謝大家的關注!

登錄后免費查看全文
立即登錄
App下載
技術鄰APP
工程師必備
  • 項目客服
  • 培訓客服
  • 平臺客服

TOP

5
1