필터 지우기
필터 지우기

Problem adding labels to figure

조회 수: 1 (최근 30일)
Juan David Parra Quintero
Juan David Parra Quintero 2023년 7월 18일
댓글: Star Strider 2023년 7월 18일
I am trying to make a figure but when I give the values to the label they are not reflected on the figure. My vector in the X axis was named as x3, however these values are not reflected in the graph. How should I proceed to do this? Thank you very much.
fig=figure(3);
clf;
x3=[Bicicleta; Microbus; Motocicleta; Vehiculo; Tractocamion];
Unrecognized function or variable 'Bicicleta'.
y3 = [1 1 1; 219 65 4; 300 828 36; 986 250 10; 0 0 10];
b3=bar(x3,y3);
ylim([0 1100])
xtips3 = b3(1).XEndPoints;
ytips3 = b3(1).YEndPoints;
labels3 = string(b3(1).YData);
text(xtips3,ytips3,labels3,'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
xtips3 = b3(2).XEndPoints;
ytips3 = b3(2).YEndPoints;
labels3 = string(b3(2).YData);
text(xtips3,ytips3,labels3,'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
xtips3 = b3(3).XEndPoints;
ytips3 = b3(3).YEndPoints;
labels3 = string(b3(3).YData);
text(xtips3,ytips3,labels3,'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
legend('Accidente','Lesionado','Muerte')
xlabel("Agente causante");
ylabel("Número de agentes");
  댓글 수: 2
Matt J
Matt J 2023년 7월 18일
편집: Matt J 2023년 7월 18일
Running your code produces errors (see above). Please post sufficient code so that it can be run in the forum and the problem reproduced.
Juan David Parra Quintero
Juan David Parra Quintero 2023년 7월 18일
Matt J muchas gracias ya pude correr el codigo. un saludo muy grande.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2023년 7월 18일
One problem is that just after creating the figure the code clears it.
Otherwise perhaps something like this —
fig=figure(3);
% clf;
x3=categorical(["Bicicleta"; "Microbus"; "Motocicleta"; "Vehiculo"; "Tractocamion"]);
y3 = [1 1 1; 219 65 4; 300 828 36; 986 250 10; 0 0 10];
b3=bar(x3,y3);
ylim([0 1100])
xtips3 = b3(1).XEndPoints;
ytips3 = b3(1).YEndPoints;
labels3 = string(b3(1).YData);
text(xtips3,ytips3,labels3,'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
xtips3 = b3(2).XEndPoints;
ytips3 = b3(2).YEndPoints;
labels3 = string(b3(2).YData);
text(xtips3,ytips3,labels3,'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
xtips3 = b3(3).XEndPoints;
ytips3 = b3(3).YEndPoints;
labels3 = string(b3(3).YData);
text(xtips3,ytips3,labels3,'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
legend('Accidente','Lesionado','Muerte', 'Location','NW')
xlabel("Agente causante");
ylabel("Número de agentes");
See the documentation for string and categorical for details on them.
.
  댓글 수: 4
Juan David Parra Quintero
Juan David Parra Quintero 2023년 7월 18일
Excelente muchisimas gracias!
Star Strider
Star Strider 2023년 7월 18일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by