Adding text labels above bars

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 Comments

The code you have shared is producing errors. Can you please correct the code so that we can run it?
  • There are 5 values for x
  • There are 6x24 values in OO
  • There are 6 values in labels
  • There are 12 data series where the color is set (either 'r' or 'b')
Perhaps an image of what the final result should be would also be helpful.
As for adding labels above bars, there are 2 examples included in the documentation:
@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.)
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;

Sign in to comment.

 Accepted Answer

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 Comments

*R2024b and later releases
The Graphics and App Building blog has an article on this topic:
(Note, the blog is currently down but will eventually become available)
@Star Strider: The LabelLocation property is meant to be used in coordination with the Labels property. If Labels is empty (which is true by default), then there are no labels, and LabelLocation has no impact.
If you've set the Labels property, only then can you use the LabelLocation property to control where those labels are placed. The current choices include end-outside and end-inside.
tiledlayout(2,2)
nexttile
bar(-2:2,Labels=["a","b","c","d","e"])
title('grouped-bars default')
nexttile
bar(-2:2,'stacked',Labels=["a","b","c","d","e"])
title('stacked-bars default')
nexttile
bar(-2:2,Labels=["a","b","c","d","e"],LabelLocation='end-outside')
title('end-outside')
nexttile
bar(-2:2,Labels=["a","b","c","d","e"],LabelLocation='end-inside')
title('end-inside')
thank you , I try your code it dosen't work , I attached the data file also example of plot .
Using your data, I did what I could to provide you with something workable. There are still significant inconsistiencies with respect to what you have and what you (apparently) want. The Labels approach will not work here because there is apparently no ability to rotate them, and that is necessary here. The XEndPoints, YEndPoints approach 'sort of' works here, however you will still need to resolve the inconsistencies. I had to reduce the font size of the labels to keep them visually separate.
Try something like this --
AB=xlsread("T1.xlsx")
AB = 6×24
0.4310 101.3300 0.4435 101.4200 0.4783 292.8500 0.1253 129.4100 0.4042 295.7200 0.3444 293.0600 0.4530 108.5100 0.4368 103.5300 0.4908 290.4100 0.2551 93.5300 0.3112 295.2000 0.2492 291.4600 0.1836 193.7600 0.1890 194.2300 0.1722 195.8200 0.0343 45.7400 0.1513 198.3400 0.1320 195.8300 0.2149 196.9100 0.2092 192.3800 0.1962 192.4000 0.0341 352.2300 0.1436 195.7800 0.1272 192.9500 0.1146 82.5700 0.1181 83.1200 0.1295 113.5900 0.0409 274.2000 0.1100 118.7700 0.0921 118.3100 0.1202 87.2000 0.1215 83.5400 0.1492 106.4400 0.1052 236.1700 0.1005 120.5300 0.0835 124.7200 0.4948 196.4400 0.4912 197.2500 0.4315 98.4200 0.0571 55.9800 0.3909 101.2800 0.3436 98.4600 0.4934 197.9400 0.4731 196.4400 0.4000 95.9100 0.0539 89.9300 0.3188 96.0000 0.2923 92.5100 0.2801 119.0900 0.2766 120.1600 0.1658 66.8900 0.0135 13.3400 0.1539 68.5900 0.1338 66.5800 0.2833 120.5600 0.2678 118.6700 0.1616 61.6200 0.0124 52.1400 0.1290 60.7100 0.1170 56.4300 0.0599 105.9400 0.0592 107.0300 0.0312 250.0200 0.0023 181.0300 0.0294 251.2300 0.0253 249.9800 0.0548 106.4200 0.0604 108.1700 0.0393 241.4400 0.0016 261.0200 0.0258 236.2700 0.0257 229.0300
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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]
OO = 12×6
0.4310 0.4435 0.4783 0.1253 0.4042 0.3444 0.4530 0.4368 0.4908 0.2551 0.3112 0.2492 0.1836 0.1890 0.1722 0.0343 0.1513 0.1320 0.2149 0.2092 0.1962 0.0341 0.1436 0.1272 0.1146 0.1181 0.1295 0.0409 0.1100 0.0921 0.1202 0.1215 0.1492 0.1052 0.1005 0.0835 0.4948 0.4912 0.4315 0.0571 0.3909 0.3436 0.4934 0.4731 0.4000 0.0539 0.3188 0.2923 0.2801 0.2766 0.1658 0.0135 0.1539 0.1338 0.2833 0.2678 0.1616 0.0124 0.1290 0.1170 0.0599 0.0592 0.0312 0.0023 0.0294 0.0253 0.0548 0.0604 0.0393 0.0016 0.0258 0.0257
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x = ["A" "B" "C" "D" "E" "F"];
labels=["north"; "south" ;"east" ;"northeast" ;"southeast" ; "west"];
figure
tiledlayout(2,1)
nexttile;
b=bar(x,OO, Labels=labels, LabelLocation='end-outside');
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)
title("Using 'Labels'")
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)
% xep = b.XEndPoints
% yep = b.YEndPoints
labels2 = [labels; labels]; % Need To Duplicate Them To Fit The Number Of Bars
for k = 1:numel(b)
xep = b(k).XEndPoints;
yep = b(k).YEndPoints;
text(xep, yep, labels2(k), Horiz='left', Vert = 'middle', Rotation=90, FontSize=5)
end
leg = legend('O','M','Location','northeast');
leg.Box = 'off';
leg.FontSize = 6;
title("Using 'XEndPoints' & 'YEndPoints'")
.
thanx I really appreciate it , its work .
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Asked:

on 4 Jun 2025

Commented:

on 15 Jun 2025

Community Treasure Hunt

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

Start Hunting!