# Labels on the top of bars for a bar plot

2 views (last 30 days)
javarocket on 27 Mar 2023
Commented: javarocket on 27 Mar 2023
Hi everyone,
I would like to do a bar plot where each bar has a different label, but labels must be different than values contained in "y" vector, because I want to show on the top of each bar the percentage difference than the bar called "25% C_ax".
The values that I would like to show are shown in vector "z" (commented).
I tried to use the code below, but it did not work.
Does anyone could help me?
Thank you very much
clear all
close all
clc
x = categorical({'Rotor only','200% C_{ax}','100% C_{ax}','50% C_{ax}','25% C_{ax}'});
x = reordercats(x,{'Rotor only','200% C_{ax}','100% C_{ax}','50% C_{ax}','25% C_{ax}'});
y = [4.521,1.5605,1.4531,1.3533,1.3528]*(1e-5);
%y = [4.521,1.5605,1.4531,1.3533,1.3528]*(1e-5);
%z = [70.0774,13.3098,6.9025,0.0369,0];
hb = bar(x,y)
%text([1:length(y)], z, num2str(z),'HorizontalAlignment','center','VerticalAlignment','bottom')
barWidth = hb.BarWidth;
numCol = size(M,2);
cnt = 0;
cnt = cnt + 1;
xPos = linspace(cnt - barWidth/2, cnt + barWidth / 2, numCol+1);
idx = 1;
for jj = xPos(1:end-1)
y = M(cnt,idx);
text(jj, y + 1, num2str(val));
idx = idx +1;
end
end
box on
grid on

Dyuman Joshi on 27 Mar 2023
clear all
close all
clc
x = categorical({'Rotor only','200% C_{ax}','100% C_{ax}','50% C_{ax}','25% C_{ax}'});
x = reordercats(x,{'Rotor only','200% C_{ax}','100% C_{ax}','50% C_{ax}','25% C_{ax}'});
y = [4.521,1.5605,1.4531,1.3533,1.3528]*(1e-5);
z = [70.0774,13.3098,6.9025,0.0369,0];
hb = bar(x,y);
text(x,y,string(z),'HorizontalAlignment','center','VerticalAlignment','bottom') javarocket on 27 Mar 2023
Thank you very much, it was easier than I tought!

### Categories

Find more on Annotations in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!