簡體   English   中英

是否可以使用histogram()而不是hist()在Matlab中並排繪制多列直方圖

[英]Is it possible to plot a multi column histogram side-by-side in Matlab using histogram() instead of hist()

使用hist()函數可以繪制: [鏈接](http://nl.mathworks.com/help/examples/graphics/CreateHistogramBarPlotwithMatrixInputExample_01.png)。

Matlab建議使用histogram()而不是hist()

使用histogram()在同一圖中繪制多個直方圖會導致: [鏈接](http://nl.mathworks.com/help/examples/matlab/PlotMultipleHistogramsExample_01.png)

列彼此重疊並且不是並排的。

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

這個怎么樣?

點擊此處查看結果圖的圖片

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

這個怎么樣?

直方圖在兩個獨立的子圖中

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM