Adding text labels above bars

3 ビュー (過去 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 Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by