MATLAB:组合和归一化具有不同样本大小的直方图

use*_*496 5 matlab distribution normalize histogram bins

我有四组数据,我想用一张图在 MATLAB 中表示它们的分布。当前代码是:

[n1,x1]=hist([dataset1{:}]);
[n2,x2]=hist([dataset2{:}]);
[n3,x3]=hist([dataset3{:}]);
[n4,x4]=hist([dataset4{:}]);
bar(x1,n1,'hist'); 
hold on; h1=bar(x1,n1,'hist'); set(h1,'facecolor','g')
hold on; h2=bar(x2,n2,'hist'); set(h2,'facecolor','g')
hold on; h3=bar(x3,n3,'hist'); set(h3,'facecolor','g')
hold on; h4=bar(x4,n4,'hist'); set(h4,'facecolor','g')
hold off 
Run Code Online (Sandbox Code Playgroud)

我的问题是我对每组有不同的抽样大小,数据集 1 的 n 为 69,数据集2 的 n 为 23,数据集 3 和数据集 4 的 n 为 10。那么,当将这三个组一起表示时,如何对分布进行归一化?

有什么方法可以..例如..通过该组的采样来划分每个 bin 中的实例?

EBH*_*EBH 2

您可以通过除以元素总数来标准化直方图:

[n1,x1] = histcounts(randn(69,1));
[n2,x2] = histcounts(randn(23,1));
[n3,x3] = histcounts(randn(10,1));
[n4,x4] = histcounts(randn(10,1));
hold on
bar(x4(1:end-1),n4./sum(n4),'histc');
bar(x3(1:end-1),n3./sum(n3),'histc');
bar(x2(1:end-1),n2./sum(n2),'histc');
bar(x1(1:end-1),n1./sum(n1),'histc');
hold off 
ax = gca;
set(ax.Children,{'FaceColor'},mat2cell(lines(4),ones(4,1),3))
set(ax.Children,{'FaceAlpha'},repmat({0.7},4,1))
Run Code Online (Sandbox Code Playgroud)

但是,正如您在上面所看到的,您可以做更多的事情来使您的代码更加简单和简短:

  1. 你只需要hold on一次。
  2. 不要收集所有bar句柄,而是使用axes句柄。
  3. 按照数据集中元素数量的升序绘制条形图,以便所有直方图都清晰可见。
  4. 使用axes手柄可通过一个命令设置所有属性。

顺便说一句 - 最好使用histcounts.

结果如下:

仅历史


编辑:

如果您还想绘制 的 pdf 线histfit,那么您可以先保存它,然后将其标准化:

dataset = {randn(69,1),randn(23,1),randn(10,1),randn(10,1)};
fits = zeros(100,2,numel(dataset));
hold on
for k = numel(dataset):-1:1
    total = numel(dataset{k}); % for normalizing
    f = histfit(dataset{k}); % draw the histogram and fit
    % collect the curve data and normalize it:
    fits(:,:,k) = [f(2).XData; f(2).YData./total].';
    x = f(1).XData; % collect the bar positions
    n = f(1).YData; % collect the bar counts
    f.delete % delete the histogram and the fit
    bar(x,n./total,'histc'); % plot the bar
end
ax = gca; % get the axis handle
% set all color and transparency for the bars:
set(ax.Children,{'FaceColor'},mat2cell(lines(4),ones(4,1),3))
set(ax.Children,{'FaceAlpha'},repmat({0.7},4,1))
% plot all the curves:
plot(squeeze(fits(:,1,:)),squeeze(fits(:,2,:)),'LineWidth',3)
hold off
Run Code Online (Sandbox Code Playgroud)

同样,您还可以对代码进行一些其他改进:

  1. 将所有内容放在一个循环中,以便以后更容易更改。
  2. 将所有曲线数据收集到一个变量,以便您可以轻松地将它们全部绘制在一起。

新的结果是:

历史与拟合