Borrar filtros
Borrar filtros

Can i add Letters above bars

2 visualizaciones (últimos 30 días)
Max1234
Max1234 el 8 de Abr. de 2023
Comentada: Alexander el 8 de Abr. de 2023
Hello guys,
I have created a bar chart and would like to write letters over the bars. I tried the same way to write the value of the bar above it, but unfortunately it doesn't work. Is this possible at all? Attached is a photo of how it should look.
Thank you very much!

Respuesta aceptada

Dyuman Joshi
Dyuman Joshi el 8 de Abr. de 2023
x=[1 2];
y=[1 3 5; 6 4 2];
b=bar(x,y);
ylim([0 8])
str={'A','B','C'};
for k=1:numel(str)
xt1=b(k).XEndPoints;
yt1=b(k).YEndPoints;
text(xt1,yt1+0.15,str{k},'HorizontalAlignment','center')
end
  1 comentario
Alexander
Alexander el 8 de Abr. de 2023
Just a little add on to your code, which makes it a bit more flexible:
x=[0:.25:1];
y=rand(5)*randi(1000,1,1);
b=bar(x,y);grid;
dy = ylim; dy = dy(2);
str={'A','B','C','D','E'};
for k=1:numel(str)
xt1=b(k).XEndPoints;
yt1=b(k).YEndPoints;
% Letter above
%htxt = text(xt1,yt1+.02*dy,str{k},'HorizontalAlignment','center','EdgeColor','none');%,'FontUnits','normalized');
% If you want to have the letter IN the bar on the top
htxt = text(xt1,yt1-.02*dy,str{k},'HorizontalAlignment','center','EdgeColor','none');%,'FontUnits','normalized');
end
Maybe the Matlab insider have a much leander solution, but it seems to work.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Bar Plots 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