2016-08-02 61 views
4

多列直方圖側由端在Matlab使用hist()功能,可以繪製該:[link](http://nl.mathworks.com/help/examples/graphics/CreateHistogramBarPlotwithMatrixInputExample_01.png).是否有可能使用直方圖()繪製的,而不是HIST()

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

後兩種方法的一些虛擬代碼(即這樣我們就可以看到你的數據結構) – Dan

回答

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

要繪製的結果很容易看到我試圖嵌入我的答案圖像,但顯然我需要至少10個聲望點才能讓我做到這一點。 –

+0

這不是我正在尋找的。我希望條形圖可以在同一個圖形內並排放置,以便更容易地比較值。 –

+1

請編輯您的答案是正確的,而不是在同一帖子上發佈其他答案。 – Suever

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