フィルターのクリア

Can i add Letters above bars

2 ビュー (過去 30 日間)
Max1234
Max1234 2023 年 4 月 8 日
コメント済み: Alexander 2023 年 4 月 8 日
Hello guys,
I have created a bar chart and would like to write letters over the bars. I tried the same way to write the value of the bar above it, but unfortunately it doesn't work. Is this possible at all? Attached is a photo of how it should look.
Thank you very much!

採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 4 月 8 日
x=[1 2];
y=[1 3 5; 6 4 2];
b=bar(x,y);
ylim([0 8])
str={'A','B','C'};
for k=1:numel(str)
xt1=b(k).XEndPoints;
yt1=b(k).YEndPoints;
text(xt1,yt1+0.15,str{k},'HorizontalAlignment','center')
end
  1 件のコメント
Alexander
Alexander 2023 年 4 月 8 日
Just a little add on to your code, which makes it a bit more flexible:
x=[0:.25:1];
y=rand(5)*randi(1000,1,1);
b=bar(x,y);grid;
dy = ylim; dy = dy(2);
str={'A','B','C','D','E'};
for k=1:numel(str)
xt1=b(k).XEndPoints;
yt1=b(k).YEndPoints;
% Letter above
%htxt = text(xt1,yt1+.02*dy,str{k},'HorizontalAlignment','center','EdgeColor','none');%,'FontUnits','normalized');
% If you want to have the letter IN the bar on the top
htxt = text(xt1,yt1-.02*dy,str{k},'HorizontalAlignment','center','EdgeColor','none');%,'FontUnits','normalized');
end
Maybe the Matlab insider have a much leander solution, but it seems to work.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeBar Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by