# MATLAB 中的直方图

## 在 MATLAB 中创建向量直方图

``````clc
vector = randn(100,1);
HG = histogram(vector)
``````

``````HG =

Histogram with properties:

Data: [100x1 double]
Values: [2 18 29 31 17 3]
NumBins: 6
BinEdges: [-3 -2 -1 0 1 2 3]
BinWidth: 1
BinLimits: [-3 3]
Normalization: 'count'
FaceColor: 'auto'
EdgeColor: [0 0 0]
``````

``````clc
vector = randn(100,1);
HG = histogram(vector);
HG.FaceColor = 'g';
HG.EdgeColor = 'r';
HG.NumBins = 10;
``````

## 在 MATLAB 中创建分类直方图

``````clc
Vector = [7 7 7 8 8 8 8 9 7 8 8 8 7 8 7 8 7 8 8 8];
Cat = categorical(Vector,[7 8 9],{'Yes','No','Not Sure'});
HG = histogram(Cat)
``````

## 在 MATLAB 中创建归一化直方图

``````clc
clc
vector = randn(100,1);
HG = histogram(vector,'Normalization','probability')
``````

## 在 MATLAB 中的同一图形上绘制多个直方图

``````clc
vector = randn(100,1);
HG1 = histogram(vector)
hold on
vector = 5+randn(100,1);
HG2 = histogram(vector)
``````

## 在 MATLAB 中保存直方图

``````savefig('Hist.fig');
close gcf
histogram = openfig('Hist.fig');
``````

## MATLAB 中的双变量直方图

``````clc
vector1 = randn(100,1);
vector2 = randn(100,1);
HG = histogram2(vector1,vector2)
``````

``````clc
vector1 = randn(100,1);
vector2 = randn(100,1);
HG = histogram2(vector1,vector2,[12 12],'FaceColor','flat')
colorbar
``````

``````clc
vector1 = randn(100,1);
vector2 = randn(100,1);
HG = histogram2(vector1,vector2,[12 12],'FaceColor','flat')
colorbar
view(2)
``````

• MATLAB 绘图线样式