gpt4 book ai didi

matlab - 是否可以使用 histogram() 而不是 hist() 在 Matlab 中并排绘制多列直方图

转载 作者:太空宇宙 更新时间:2023-11-03 20:08:05 24 4
gpt4 key购买 nike

使用 hist() 函数可以绘制:[link](http://nl.mathworks.com/help/examples/graphics/CreateHistogramBarPlotwithMatrixInputExample_01.png).

Matlab 推荐使用 histogram() 而不是 hist()

使用 histogram() 在同一图中绘制多个直方图会导致:[link](http://nl.mathworks.com/help/examples/matlab/PlotMultipleHistogramsExample_01.png)

各列相互重叠,不是并排的。

是否可以使用 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)

最佳答案

这个怎么样?

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

关于matlab - 是否可以使用 histogram() 而不是 hist() 在 Matlab 中并排绘制多列直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38714997/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com