Adding text labels above bars

3 visualizaciones (últimos 30 días)
eb.az
eb.az el 4 de Jun. de 2025
Comentada: Star Strider el 15 de Jun. de 2025
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 comentarios
Star Strider
Star Strider el 4 de Jun. de 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.)
Cris LaPierre
Cris LaPierre el 4 de Jun. de 2025
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;

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 4 de Jun. de 2025
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 comentarios
eb.az
eb.az el 14 de Jun. de 2025
thanx I really appreciate it , its work .
Star Strider
Star Strider el 15 de Jun. de 2025
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by