Can i add Letters above bars

2 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Bar Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by