您可以通过除以元素总数来标准化直方图:
[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))
但是,正如您在上面看到的,您可以做更多的事情来使您的代码更简单和简短:
- 你只需要
hold on
一次。
- 不要收集所有的
bar
句柄,而是使用axes
句柄。
- 按数据集中元素数量的升序绘制条形图,以便所有直方图清晰可见。
- 使用
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
同样,您可以在代码中引入一些其他改进:
- 将所有内容放在一个循环中,以使以后更容易更改。
- 将所有曲线数据收集到一个变量中,以便您可以非常轻松地将它们全部绘制在一起。
新结果是:
