text to stack bar

2 views (last 30 days)
Prasad Joshi
Prasad Joshi on 13 Apr 2022
Commented: Mathieu NOE on 13 Apr 2022
Hello ,
I am thinking of adding text to stack bar but its not working can someone suggest improvement in code . where i am going wrong. Thanks in advance like each value should be in that mark only eg :- 1051 in red , 1427 in blue mark & 1580 in red mark
clear all
close all
clc
x=input('Enter the first column:','s')
r=xlsread('Master_data.xlsx',strcat(x,':E',num2str((str2num(x(2:strlength(x)))+8))))
i=1
C(1,:)=[r(i+1) r(i+10) r(i+19) r(i+28) r(i+2) r(i+11) r(i+20) r(i+29)]
C(2,:)=[r(i+4) r(i+13) r(i+22) r(i+31) r(i+5) r(i+14) r(i+23) r(i+32)]
C(3,:)=[r(i+7) r(i+16) r(i+25) r(i+34) r(i+8) r(i+17) r(i+26) r(i+35)]
D(1,:)=C(1,:)
D(2,:)=C(2,:) -(C(1,:))
D(3,:)=C(3,:)-(C(2,:))
D=D'
h=figure
hB=bar(D,'stacked')
set(hB(1),'Facecolor','red')
set(hB(2),'Facecolor','blue')
set(hB(3),'Facecolor','green')
legend('10 sec','25 sec', '60 sec')
xt = get(gca, 'XTick');
set(gca, 'XTick', xt, 'XTickLabel', {'C1-Case1' 'C1-Case2' 'C1-Case3' 'C1-Case4' 'C2-Case1' 'C2-Case2' 'C2-Case3' 'C2-Case4'},'fontsize',12)
title('Cumulative CO emission[mg] ','fontsize',12);
ylabel('Cumulative CO emission[mg]','FontSize',12)
labels_stacked=num2str(C(:,1),'%.1f ');
hText = text(1:size(C), C(:,1), labels_stacked);
set(hText, 'VerticalAlignment','top', 'HorizontalAlignment', 'center','FontSize',12, 'Color','k');
  2 Comments
Mathieu NOE
Mathieu NOE on 13 Apr 2022
hi
the y coordinates of the text ( = C(:,1) in your code) is not correct if you want the text to appear below the top of each color zone (or in the middle)
all the best

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!