2017-05-01 139 views
2

我有一個分組條形圖,我想比較這些值.i.e,我的意思是我想用線將其可視化。我嘗試下面的代碼和輸出也遵循如何繪製分組條形圖頂部的一條線?

Y=rand(5,5) 
str = {'A'; 'B'; 'C'; 'D'; 'E';}; 
bar_widh=0.2; 
h = bar(Y,bar_widh); 
hold on;plot(Y,'b'); 
set(gca, 'XTickLabel',str, 'XTick',1:numel(str)) 
grid on 
l = cell(1,5); 
l{1}='P'; l{2}='Q'; l{3}='R'; l{4}='S'; l{5}='T'; 
legend(h,l); 

我得到了以下的輸出:

enter image description here

我想以可視化的bar.In的最小數量/數量較大的情況下,一些價值較大不好。你能幫我繪製線的顏色一樣的吧

我得到的輸出如下

enter image description here

+0

有什麼不對您的電流輸出? – m7913d

+0

@ m7913d線不會添加到酒吧的頂部,但在A,B,C,D和E .. – Anthony

+0

實際上我想要可視化哪個更小 – user

回答

2

你可以試試這個:

Y=rand(5,5); 
str = {'A'; 'B'; 'C'; 'D'; 'E';}; 
bar_widh=0.2; 

figure; hold on;grid on 
h = bar(Y,bar_widh); 

% to highlight the minimum of each group, 
% copy data into a new matrix 
Y_ = Y; 
% find the minimum values and make the rest zeors 
Y_(Y_~=repmat(min(Y_,[],1),size(Y,1),1)) = 0; 
% then plot with so sort of highlighting 
h2 = bar(Y_,0.5); 

pause(0.1) % pause to allow bars to be drawn 

% now go through each group of bars and plot the line 
for i = 1:numel(h) 
    x = h(i).XData + h(i).XOffset; % find the x coordinates where the bars are plotted 
    ax = plot(x,Y(:,i)); % plot the line 
    % set color of the bars the same as the line 
    h(i).FaceColor = ax.Color; 
    h2(i).FaceColor = ax.Color; 
end 

set(gca, 'XTickLabel',str, 'XTick',1:numel(str)) 
legend('P','Q','R','S','T'); 

h(i).XData

是第i組鋼筋的中心座標。

例如,在你的情況:

h(1).XData = [ 1 2 3 4 5 ]; % group P 
h(2).XData = [ 1 2 3 4 5 ]; % group Q 
... 
h(5).XData = [ 1 2 3 4 5 ]; % group T 

h(i).XOffset

是從其對應的中心座標組中的每個杆的偏移值。

例如,你的情況:

h(1).XOffset = -0.3077; % group P 
h(2).XOffset = -0.1538; % group Q 
... 
h(5).XOffset = 0.3077; % group T 

沒有突出的最小值 enter image description here

最小值強調 enter image description here

+1

總結鍵值可能很有用部分解決方案並提供解決方案的屏幕截圖。 – m7913d

+1

@Anthony是否可以突出顯示每組條形圖的最小第一個值(即ABCDE的第一個欄中的最小值第一個值爲 – user

+1

@user我已更新代碼 – Anthony