Adding text labels above bars
Afficher commentaires plus anciens
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 commentaires
Cris LaPierre
le 4 Juin 2025
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:
- Create bar labels (since R2024b)
- Create bar labels using text function
Star Strider
le 4 Juin 2025
@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.)
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;
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Environment and Settings dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





