2014-01-23 68 views
4

我知道如何繪製grouped條形圖和stacked柱狀圖如下:將「分組」和「堆疊」結合到BAR圖中?

Y = round(rand(5,4)*10); 
figure; 
subplot(2,2,1); bar(Y,'grouped'); title('Group') % similar for 'hist'/'histc' 
subplot(2,2,2); bar(Y,'stacked'); title('Stack') 

這將產生類似:

enter image description here

我的問題是,我怎麼能結合選項groupedstacked在BAR圖中能夠生成如下圖形(堆疊3,4一起)?或者有沒有其他方法可以實現這一目標?附:我手動繪製了下圖。

enter image description here

+0

可能重複http://stackoverflow.com/問題/ 6012568/how-can-i-create-a-barseries-plot-using-both-grouped-and-stacked-styles-in-matla – hesar

+0

@hesar它不是重複的。該問題涉及繪製堆積條形圖組,即以分組佈局繪製堆積條形圖。我的問題是同時顯示兩者。不管怎麼說,還是要謝謝你。我會試圖弄清楚他們的想法在這裏如何應用。 – herohuyongtao

回答

3

我終於找到了一個辦法做到這一點,這個想法是:疊置條柱的

  1. 劇情組(需要plotBarStackGroups.m)。

  2. 設置額外的零來模擬原始組。

結合這些結合在一起,代碼將是這樣的:

Y = round(rand(5,3,2)*10); 
Y(1:5,1:2,1) = 0; % setting extra zeros to simulate original groups. 

groupLabels = { 1, 2, 3, 4, 5};  % set labels 
plotBarStackGroups(Y, groupLabels); % plot groups of stacked bars 

而其結果將是這樣的:

enter image description here