Labels on the top of bars for a bar plot

조회 수: 8 (최근 30일)
javarocket
javarocket 2023년 3월 27일
댓글: javarocket 2023년 3월 27일
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")

채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 3월 27일
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
javarocket
javarocket 2023년 3월 27일
Thank you very much, it was easier than I tought!

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by