How to label stacked bar?

조회 수: 20 (최근 30일)
Benedict Low
Benedict Low 2017년 3월 6일
댓글: Benedict Low 2017년 3월 29일
Hi,
I have a stacked bar that I would like to label, but it is turning out more frustrating than I thought. I basically want to label each portion of the stacked bars.
It is an 11 by 5 matrix of the distance covered by each footballer while walking, jogging, running, high-speed running and sprinting.
The basics of the code that I use is as follows, and a sample of the stacked bar is shown in the picture
figure; bar(rand(11,5), 'stacked');
set(hText, 'VerticalAlignment','bottom', 'HorizontalAlignment', 'center','FontSize',16, 'Color','k');
hLegend = legend(bar(Belgium_TimeMotion_ascending(:,1:5), 'stacked'), {'Walking (<2m/s)','Jogging (2 to 4m/s)','Running (4 to 5.5m/s)','High-speed running (5.5 to 7m/s)','Sprinting (>7m/s)'});
set(hLegend, 'Location','Best','FontSize',10);
legend('boxoff');
Is there a syntax I can use to
  1. label the portions of the stacked bars?
  2. set a conditional statement not to display any zero values (e.g. player 1 did not do any high-speed running or sprinting, so figure does not need to display "zero" two times.
Any help is appreciated. Thank you!
  댓글 수: 2
Rik
Rik 2017년 3월 6일
Have a look how to correctly format your code here (in the answers there is also an animated gif).
If you want to label all bar parts, can't you use the text function? It is tricky to get it to look nice, especially with a scaling figure, but it sounds closest to what you want.
Benedict Low
Benedict Low 2017년 3월 13일
Hi Rik, thank you for your advice, I have tried the text function but I have one minor problem, as seen in the picture, the zero values overlapped with some of the preceding values.(the topmost white text)
The additional code I've written is
labels_stacked=num2str(sample_data(:,1),'%.1f m');
hText = text(1:size(sample_data), sample_data(:,1), labels_stacked);
% Repeated 5 times for an 11 by 5 matrix
labels_stacked=num2str(sample_data(:,2),'%.1f m');
hText = text(1:size(sample_data), sum(sample_data(:,1:2),2), labels_stacked);
labels_stacked=num2str(sample_data(:,3),'%.1f m');
hText = text(1:size(sample_data), sum(sample_data(:,1:3),2), labels_stacked);
labels_stacked=num2str(sample_data(:,4),'%.1f m');
hText = text(1:size(sample_data), sum(sample_data(:,1:4),2), labels_stacked);
labels_stacked=num2str(sample_data(:,5),'%.1f m');
hText = text(1:size(sample_data), sum(sample_data(:,1:5),2), labels_stacked);
Is there a conditional statement for the text function not to display if some elements of the matrix have zero value?
Any help is appreciated. Thank you.

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
Rik 2017년 3월 14일
편집: Rik 2017년 3월 15일
I did not test this code, but it should skip the 0 values
hText=zeros(1,size(sample_data,2));
for n=1:size(sample_data,2)
labels_stacked=num2str(sample_data(:,n),'%.1f m');
textheight=sum(sample_data(:,1:n),2);
text_x=1:size(sample_data);
non_zero=sample_data(:,n)~=0;
hText(n) = text(text_x(non_zero), textheight(non_zero), labels_stacked(non_zero,:));
end
Are you planning to do something with hText? If so, you should think about making a vector/matrix of handles.
  댓글 수: 3
Rik
Rik 2017년 3월 15일
I wrongly assumed labels_stacked to be a cell, so I'll edit my answer.
Benedict Low
Benedict Low 2017년 3월 29일
Hi Rik,
Thanks for your response. I've edited it a little and basically found a way to do a number to string conversion, only if an element of an array is >0
% Sample 5 by 5 data
sample_data=[3 5 3 3 3; 2 8 2 2 0; 1 1 1 0 0; 2 2 0 0 0; 7 3 2 4 0];
% Plotting a stacked bar chart
figure; bar(sample_data, 'stacked');
% A loop that does num2str conversion only if value is >0
for i=1:size(sample_data,1);
for j=1:size(sample_data,2);
if sample_data(i,j)>0;
labels_stacked=num2str(sample_data(i,j),'%.1f m');
hText = text(i, sum(sample_data(i,1:j),2), labels_stacked);
set(hText, 'VerticalAlignment','top', 'HorizontalAlignment', 'center','FontSize',10, 'Color','w');
end
end
end
The resulting plot is as follows.
Thanks for all your help!
Best regards,
Ben

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by