MATLAB直方圖函數-histogram
h =
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)
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)
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)
通過顯式設置 bin 數按精細顆粒級別調整 bin。
h.NumBins = 31;
生成 1,000 個隨機數并創建直方圖。將 bin 邊界指定為向量,使寬 bin 在直方圖的兩邊,以捕獲不滿足 |x|<2 的離群值。第一個向量元素是第一個 bin 的左邊界,而最后一個向量元素是最后一個 bin 的右邊界。
x = randn(1000,1);
edges = [-10 -2:0.25:2 10];
h = histogram(x,edges);
h.Normalization = 'countdensity';
創建一個表示投票的分類向量。該向量中的類別是 '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)
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')
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);
h1.Normalization = 'probability';
h1.BinWidth = 0.25;
h2.Normalization = 'probability';
h2.BinWidth = 0.25;
生成 1,000 個隨機數并創建直方圖。返回直方圖對象以調整該直方圖的屬性,無需重新創建整個繪圖。
x = randn(1000,1);
h = histogram(x)
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;
通過向量指定 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')
均值為 μ、標準差為 σ 以及方差為 σ^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)
使用 savefig 函數保存直方圖。
y = histogram(randn(10));
savefig('histogram.fig');
clear all
close all
使用 openfig 重新將直方圖加載到 MATLAB。openfig 也返回圖窗 h 的句柄。
h = openfig('histogram.fig');
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
【免責聲明】本文檔部分內容摘自網絡平臺,版權歸原作者所有,僅用于技術分享與交流,非商業用途!若有涉及版權等請告知,將及時修訂刪除,謝謝大家的關注!
工程師必備
- 項目客服
- 培訓客服
- 平臺客服
TOP




















