Labels on the top of bars for a bar plot

7 ビュー (過去 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 Center および File ExchangeAnnotations についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by