5

我有四组数据,我想在一张图中用 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 

我的问题是我对每个组有不同的采样大小,dataset1 的 n 为 69,dataset2 的 n 为 23,dataset3 和 dataset4 的 n 为 10。那么当将这三个组表示在一起时,如何对分布进行归一化?

有没有办法..例如..将每个箱中的实例除以该组的采样?

4

1 回答 1

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))

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

  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

同样,您可以在代码中引入一些其他改进:

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

新结果是:

组织与拟合

于 2017-02-14T20:55:20.050 回答