使用hist()
功能可以绘制:The code
Matlab建议使用histogram()
代替hist()
。
使用histogram()
在同一图表中绘制多个直方图会导致:
列彼此重叠,并非并排。
是否可以使用histogram()
函数在同一图中并排显示列并排的直方图?如果是,我该怎么做?
代码段:
a = randn(100, 2);
edges = -3:3;
xbins = edges(1:end-1)+0.5;
figure(1)
hist(a, xbins)
figure(2), hold on
histogram(a(:, 1), edges)
histogram(a(:, 2), edges)
答案 0 :(得分:1)
这个怎么样?
Click here for a picture of the resulting plot
data1 = randn(1000,1);
data2 = randn(1000,1);
data2 = data2 - 1.5*ones(size(data2));
lowest_boundary = min(min(data1), min(data2));
highest_boundary = max(max(data1), max(data2));
nbins = 10;
boundaries = linspace(lowest_boundary, highest_boundary, nbins + 1);
bin_assighnments1 = discretize(data1, boundaries);
bin_assighnments2 = discretize(data2, boundaries);
bin_counts1 = zeros(numel(boundaries) - 1, 1);
bin_counts2 = zeros(numel(boundaries) - 1, 1);
for m = 1:numel(bin_assighnments1)
n = bin_assighnments1(m);
bin_counts1(n) = 1 + bin_counts1(n);
n = bin_assighnments2(m);
bin_counts2(n) = 1 + bin_counts2(n);
end
merged_bin_counts = cat(2, bin_counts1, bin_counts2);
x = zeros(1, nbins);
for m = 1:nbins
x(m) = (boundaries(m) + boundaries(m+1))/2;
end
bar(x, merged_bin_counts);
答案 1 :(得分:-1)
这个怎么样?
histograms in two separate subplots
x1 = randn(100,1);
x2 = randn(100,1);
figure_rows = 1;
figure_cols = 2;
figure
subplot(figure_rows, figure_cols, 1);
histogram(x1);
title('Hist One')
subplot(figure_rows, figure_cols, 2);
histogram(x2);
title('Hist Two')