2017-01-01 93 views
1

我想在單個繪圖中繪製多個直方圖,其中直方圖沿第三個軸分開。沿第三軸繪製多個歸一化直方圖

舉例來說,假設我想繪製以下兩個直方圖(在現實中,我想在同一時間繪製像10個直方圖):

enter image description here

enter image description here

它們是由下面的代碼生成:

histogram(points_inside1,100,'Normalization','pdf') 

histogram(points_inside2,100,'Normalization','pdf') 

其中 'points_inside1' 和 'points_inside2' 是具有10^5項陣列。

我不能得到bar3工作(我不認爲它可以把規範化的直方圖作爲輸入)。

謝謝。

+0

@Royi當然! –

回答

1

要做到這一點吧,你需要通過histcounts首先得到直方圖數據,然後用bar3繪製它:

% some data: 
p1 = randn(1000,1)+2; 
p2 = randn(1000,1); 
% don't use too many bins: 
bins = 10; 
% get the histogram data: 
[N1] = histcounts(p1,bins,'Normalization','pdf'); 
[N2] = histcounts(p2,bins,'Normalization','pdf'); 
% plot it: 
bar3([N1.' N2.']); 

然而,你可能仍然有一個問題,因爲X軸未對齊所有的直方圖之間,這樣你就可以使用合併後的直方圖的edges對於所有其他:

% get the histogram data: 
[~,edges] = histcounts([p1;p2],bins,'Normalization','pdf'); 
[N1] = histcounts(p1,edges,'Normalization','pdf'); 
[N2] = histcounts(p2,edges,'Normalization','pdf'); 
% compute the correct X-tick values: 
X = movmean(edges.',2); 
% plot it: 
b = bar3(X(2:end),[N1.' N2.']); 

3D_hist