Clear Filters
Clear Filters

Can i add Letters above bars

2 views (last 30 days)
Max1234
Max1234 on 8 Apr 2023
Commented: Alexander on 8 Apr 2023
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!

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 8 Apr 2023
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 Comment
Alexander
Alexander on 8 Apr 2023
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.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!