📜  如何在 MATLAB 中绘制直方图?

📅  最后修改于: 2022-05-13 01:54:46.083000             🧑  作者: Mango

如何在 MATLAB 中绘制直方图?

直方图是用户指定范围内的一组数据的图形表示。基本上,直方图包含几个 bin。箱是数据分布的非重叠间隔。在 MATLAB 中,我们有一个名为的函数 hist()允许我们绘制条形图。

句法:

hist(X)
where X represents the data. The X is a vector.

histogram函数使用一种算法,该算法返回 bins 和 bins 宽度相等。这些箱根据向量中给出的数据展开。有趣的是,每个 bin 的高度表示该 bin 中的点数。

现在让我们来看一些例子。

示例 1:一个简单的直方图:



MATLAB
% generate 10,000 random numbers
y=randn(10000,1)
 
% hist() function to plot the histogram
% if the value of bins is not given then
% this function choose appropriate numbers
% of bin
hist(y)


MATLAB
% generate 10,000 random numbers
y=randn(10000,1)
 
% assign 50 to variable nbins for
% number of bins
nbins=50;
 
% hist() function to plot the histogram
% hist() function use the nbins variable
% and spread the data in 50 bins
hist(y,nbins)


MATLAB
% generate 10,000 random numbers in 4 groups
y=randn(10000,4)
 
% hist() function to plot the histogram
% hist() function use the nbins variable and
% spread the data in 50 bins
hist(y)


MATLAB
% read the image
I = imread('ngc6543a.jpg');
 
% display the image
imshow(I)


MATLAB
% imhist() function is used to
% draw histogram of image
imhist(I)


输出:

示例 2:具有给定 bin 数量的直方图:



MATLAB

% generate 10,000 random numbers
y=randn(10000,1)
 
% assign 50 to variable nbins for
% number of bins
nbins=50;
 
% hist() function to plot the histogram
% hist() function use the nbins variable
% and spread the data in 50 bins
hist(y,nbins)

输出:

示例 3:多列的直方图:



MATLAB

% generate 10,000 random numbers in 4 groups
y=randn(10000,4)
 
% hist() function to plot the histogram
% hist() function use the nbins variable and
% spread the data in 50 bins
hist(y)

输出 :

示例 4:制作图像直方图:

MATLAB



% read the image
I = imread('ngc6543a.jpg');
 
% display the image
imshow(I)


MATLAB

% imhist() function is used to
% draw histogram of image
imhist(I)

输出 :