how to assign names to each bar of a bar chart

607 visualizaciones (últimos 30 días)
Kirk
Kirk el 28 de Mzo. de 2012
Comentada: ZenithWoman el 27 de Jun. de 2021
Hello, Suppose i have some data that i want to plot in a bar chart. How do i assign labels to each of the bars. e.g.
somedata=randn(1,3)
somenames={'mike'; 'steve'; 'alex' }
barh(somedata)
My question is, how do i label each of the bars to the extreme left with the names.
Thank you for your help.

Respuesta aceptada

C.J. Harris
C.J. Harris el 28 de Mzo. de 2012
You can simply add the names as the y-tick labels.
somedata=randn(1,3);
somenames={'mike'; 'steve'; 'alex' };
barh(somedata)
set(gca,'yticklabel',somenames)
  1 comentario
emjey
emjey el 17 de Mayo de 2019
Editada: emjey el 17 de Mayo de 2019
But that doens't work if you have more than 20-30 data records :(
In such case Matlab plots the first few labels only...

Iniciar sesión para comentar.

Más respuestas (2)

ORUGANTI SIVAMURALIKRISHNA
ORUGANTI SIVAMURALIKRISHNA el 5 de Jul. de 2020
Use like this
X = categorical({'Small','Medium','Large','Extra Large'});
Y = [0 15 20 25];
bar(X,Y)
  2 comentarios
Bill Tubbs
Bill Tubbs el 15 de Jun. de 2021
Is there a reason why it sorts the data along the x-axis in alphabetical order? Is there any way to preserve the order?
ZenithWoman
ZenithWoman el 27 de Jun. de 2021
To preserve the order write
Write like this
X = categorical({'Small','Medium','Large','Extra Large'});
X = reordercats(X,{'Small','Medium','Large','Extra Large'});
Y = [0 15 20 25];
bar(X,Y)
I hope this helps

Iniciar sesión para comentar.


Sarul Gupta
Sarul Gupta el 4 de Mayo de 2017
Thanks :)

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by