如何在 MATLAB 中绘制直方图?
直方图是一种数据图形表示,显示数据点的频率分布。MATLAB 提供了一个内置函数“histogram”来绘制数据集的直方图。
什么是 MATLAB 中的直方图?
在 MATLAB 中,直方图是一种以图形方式表示一组数据点频率分布的方法。它显示了数据点在称为 bin 的特定区间内的频率分布。
在直方图中,区间或 bin 在 x 轴上表示,数据点的频率在 y 轴上表示。在 MATLAB 中,直方图是图形化表示和分析数据的一种有效方法。
有一个名为“histogram”的内置函数,我们可以使用它来绘制 MATLAB 中数据集的直方图。此函数具有不同的语法格式,可以根据需要绘制各种类型的直方图。
现在让我们讨论 MATLAB 中“histogram”函数的各种语法格式。
(1). 使用默认属性绘制直方图
在 MATLAB 中,以下“histogram”函数的语法用于使用默认属性绘制数据集的直方图。
histogram(X);
这里,X 是数据点的向量。
以下示例演示了如何使用此函数创建直方图。
示例(1)
% MATLAB code to plot default histogram % Create a sample data set X = randn(5000, 2); % Plot the histogram for the data set histogram(X);
输出

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
(2). 使用指定的 bin 数绘制直方图
以下“histogram”函数的语法用于绘制具有指定 bin 数或区间的直方图。
histogram(X, bins);
这里,X 是数据集的向量,“bins”指定直方图的区间数。
以下 MATLAB 代码显示了此函数的实现,
示例(2)
% MATLAB code to plot histogram with specified number of bins % Create a sample data set X = randn(5000, 2); % Plot the histogram for the data set histogram(X, 25);
输出

(3). 使用指定的 bin 边界绘制直方图
要绘制具有指定 bin 边界的直方图,请使用以下“histogram”函数的语法。
histogram(X, edges);
这里,X 是包含数据集的向量,“edges”是包含 bin 边界的向量。
让我们看一个示例来了解此函数的实现。
示例(3)
% MATLAB code to plot histogram with specified edges of the bins % Create a sample data set X = randn(5000, 2); % Create a vector containing edges of bins edges = [-5 -4 -3 -2 -1 0 1 2 3 4 5]; % Plot the histogram for the data set histogram(X, edges);
输出

(4). 绘制分类数据的直方图
以下“histogram”函数的语法用于绘制分类数据的直方图。
histogram(C);
这里,C 是分类数据。
示例(4)
以下示例演示了此函数的实现。
% MATLAB code to plot histogram of categorical data % Create a sample array of categories cat = {'X', 'Y', 'Z', 'X', 'Y', 'X', 'Z'}; % Create categorical data C = categorical(cat); % Plot the histogram for the categorical data histogram(C);
输出

(5). 使用指定的类别绘制分类数据的直方图
以下“histogram”函数的语法用于绘制具有指定类别的分类数据的直方图。
histogram(C, Categories);
让我们看一个基于此函数的示例。
示例(5)
% MATLAB code to plot histogram of categorical data with specified categories % Create a sample array of categories cat = {'X', 'Y', 'Z', 'X', 'Y', 'X', 'Z'}; % Create categorical data C = categorical(cat); % Plot the histogram for the categorical data histogram(C,{'X', 'Y', 'Z', 'A'});
输出

(6). 使用指定的类别和预计算的 bin 计数绘制分类数据的直方图
以下“histogram”函数的语法用于绘制具有指定类别和预计算的 bin 计数的分类数据的直方图。
histogram('Categories', Categories, 'BinCounts', counts);
示例(6)
这是一个演示此函数实现的示例。
% MATLAB program to plot categorical histogram with prespecified categories and bin counts % Create sample categories cat = {'X', 'Y', 'Z', 'X', 'Y', 'X', 'Z'}; % Calculate bin counts for each category counts = [3, 2, 1, 2]; % Plot the histogram histogram('Categories', {'X', 'Y', 'Z', 'A'}, 'BinCounts', counts);
输出

(7). 使用自定义属性绘制直方图
以下“histogram”函数的语法用于使用自定义属性绘制直方图。
histogram(___, Name, Value);
这里,直方图的自定义属性使用名称-值对指定。
示例(7)
让我们看一个示例来了解如何实现此函数。
% MATLAB program to plot a histogram with customized properties % Create a sample data set X = randn(5000, 2); % Plot the histogram with customized properties histogram(X, 'FaceColor', 'blue', 'BinWidth', 0.75);
输出

(8). 使用指定的轴绘制直方图
以下“histogram”函数的语法用于使用指定的轴绘制直方图。
histogram(ax, ___);
示例(8)
以下 MATLAB 示例演示了如何使用此函数绘制直方图。
% MATLAB code to plot a histogram with specified axes % Create a sample data set X = randn(5000, 2); % Specify the subplot axes ax = subplot(1, 1, 1); % Plot the histogram in the subplot histogram(ax, X);
输出

结论
MATLAB 有一个内置函数“histogram”来绘制数据集的直方图。在本教程中,我已经解释了“histogram”函数的不同语法格式,具体取决于不同类型的数据和输出要求。