How to label stacked bar?
    21 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
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
- label the portions of the stacked bars?
- 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 Commenti
  Rik
      
      
 il 6 Mar 2017
				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.
Risposta accettata
  Rik
      
      
 il 14 Mar 2017
        
      Modificato: Rik
      
      
 il 15 Mar 2017
  
      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 Commenti
Più risposte (0)
Vedere anche
Categorie
				Scopri di più su Annotations in Help Center e File Exchange
			
	Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


