Filter löschen
Filter löschen

Problem adding labels to figure

1 Ansicht (letzte 30 Tage)
Juan David Parra Quintero
Juan David Parra Quintero am 18 Jul. 2023
Kommentiert: Star Strider am 18 Jul. 2023
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 Kommentare
Matt J
Matt J am 18 Jul. 2023
Bearbeitet: Matt J am 18 Jul. 2023
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 am 18 Jul. 2023
Matt J muchas gracias ya pude correr el codigo. un saludo muy grande.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Star Strider
Star Strider am 18 Jul. 2023
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 Kommentare
Juan David Parra Quintero
Juan David Parra Quintero am 18 Jul. 2023
Excelente muchisimas gracias!
Star Strider
Star Strider am 18 Jul. 2023
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Colormaps finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by