Labels on the top of bars for a bar plot

6 vues (au cours des 30 derniers jours)
javarocket
javarocket le 27 Mar 2023
Commenté : javarocket le 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')
numbersToAdd = [70.0774,13.3098,6.9025,0.0369,0];
barWidth = hb.BarWidth;
numCol = size(M,2);
cnt = 0;
for ii = numbersToAdd'
cnt = cnt + 1;
xPos = linspace(cnt - barWidth/2, cnt + barWidth / 2, numCol+1);
idx = 1;
for jj = xPos(1:end-1)
val = numbersToAdd(cnt,idx);
y = M(cnt,idx);
text(jj, y + 1, num2str(val));
idx = idx +1;
end
end
box on
grid on
ylabel("Work on the blade, J")

Réponse acceptée

Dyuman Joshi
Dyuman Joshi le 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')
  1 commentaire
javarocket
javarocket le 27 Mar 2023
Thank you very much, it was easier than I tought!

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by