필터 지우기
필터 지우기

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 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