Adding text labels above bars

조회 수: 2 (최근 30일)
eb.az
eb.az 2025년 6월 4일
댓글: Star Strider 2025년 6월 15일
Hi,
I have a problem writing specific text above a bar chart (labels). Here's my code.
AB=xlsread("T1.xlsx")
A1=AB(1,1:2:12);
B1=AB(2,1:2:12);
C1=AB(3,1:2:12);
D1=AB(4,1:2:12);
E1=AB(5,1:2:12);
F1=AB(6,1:2:12);
A2=AB(1,13:2:24);
B2=AB(2,13:2:24);
C2=AB(3,13:2:24);
D2=AB(4,13:2:24);
E2=AB(5,13:2:24);
F2=AB(6,13:2:24);
OO=[A1;A2;B1;B2;C1;C2;D1;D2;E1;E2;F1;F2];
x = ["A" "B" "C" "D" "F"];
labels=["north"; "south" ;"east" ;"northeast" ;"southeast" ; "west"];
figure
tiledlayout(2,1)
nexttile;
b=bar(x,OO)
set(b(1),'facecolor','b')
set(b(2),'facecolor','r')
set(b(3),'facecolor','b')
set(b(4),'facecolor','r')
set(b(5),'facecolor','b')
set(b(6),'facecolor','r')
set(b(7),'facecolor','b')
set(b(8),'facecolor','r')
set(b(9),'facecolor','b')
set(b(10),'facecolor','r')
set(b(11),'facecolor','b')
set(b(12),'facecolor','r')
set(gca,'ylim',[0 1])
set(gca,'ytick',0:0.1:1)
leg = legend('O','M','Location','northeast');
leg.Box = 'off';
leg.FontSize = 6;
I've attached the data file
Can anyone help me?
I want to plot the same figure
  댓글 수: 3
Star Strider
Star Strider 2025년 6월 4일
@Cris LaPierre -- I would appreciate your providing an example using LabelLocation to plot bar labels, specificallly text labels. I saw it in the documentation on Bar Properties, however there is no example provided, and it's use is not obvious . (Adding that bit of information to the documentation would also be appreciated.)
Cris LaPierre
Cris LaPierre 2025년 6월 4일
The example I linked to is on the documentation page for bar.
Here is the code you shared updated to use Labels (not LabelLocation) to add text labels.
x = ["A" "B" "C" "D" "E" "F"];
labels=["north"; "south" ;"east" ;"northeast" ;"southeast" ; "west"];
y = rand(1,numel(labels));
figure
hb = bar(x, y);
hb.Labels = labels;

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2025년 6월 4일
A lot of aspects of your code co not match the numbers of labels that you have. Adjucting various aspects of it make other aspects no longer work. (I leave those to you to resolve.)
That aside, in R2025b and later releases, you can use the LabelLocation function. However, since thare appear to be no instructions or example code on how to use it, a more general option would be to use XEndPoints and YEndPoints with an appropriate text call.
I cannot illustrate that with your code, because of the inconsistiencies in it, so I added a simpler version at the end.
x = ["A" "B" "C" "D" "F"];
labels=["north"; "south" ;"east" ;"northeast" ;"southeast" ; "west"];
OO=rand(6,24);
PP=rand(6,24);
figure
tiledlayout(2,1)
nexttile;
% b=bar(x,OO)
% set(b(1),'facecolor','b')
% set(b(2),'facecolor','r')
% set(b(3),'facecolor','b')
% set(b(4),'facecolor','r')
% set(b(5),'facecolor','b')
% set(b(6),'facecolor','r')
% set(b(7),'facecolor','b')
% set(b(8),'facecolor','r')
% set(b(9),'facecolor','b')
% set(b(10),'facecolor','r')
% set(b(11),'facecolor','b')
% set(b(12),'facecolor','r')
% set(gca,'ylim',[0 1])
% set(gca,'ytick',0:0.1:1)
% legend({'Model','Observvation'},'Location','northeast')
% leg = legend('O','M','Location','northeast');
% leg.Box = 'off';
% leg.FontSize = 6;
A related illustration --
x = 1:numel(labels);
y = rand(1,numel(labels));
figure
hb = bar(x, y);
xep = hb.XEndPoints;
yep = hb.YEndPoints;
text(xep, yep, labels, Vert='bottom', Horiz='center')
ylim([min(ylim) max(ylim)+0.1])
.
  댓글 수: 7
eb.az
eb.az 2025년 6월 14일
thanx I really appreciate it , its work .
Star Strider
Star Strider 2025년 6월 15일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by