How to create bar graph with categorical data
Mostrar comentarios más antiguos
I would like to plot a bar graph separated in categories. I tried out the example code here from Matlab Documentation:
c = categorical({'apples','oranges','pears'});
prices = [1.23 0.99 2.3];
bar(c,prices)
But I don't get the categories displayed on the "x-Axis" of the graph, instead just int's 1-3.

1 comentario
Xeozim
el 3 de Nov. de 2017
Also seeing this behaviour in R2016a
Respuesta aceptada
Más respuestas (1)
Chaman Dewangan
el 13 de Jul. de 2021
I found answer in mathworks.com as. Very happy it is working nicely.
One way to indicate categories for your bars is to specify X as a categorical array. The bar function uses a sorted list of the categories, so the bars might display in a different order than you expect. To preserve the order, call the reordercats function.
Define X as categorical array, and call the reordercats function to specify the order for the bars. Then define Y as a vector of bar heights and display the bar graph.
X = categorical({'Small','Medium','Large','Extra Large'});
X = reordercats(X,{'Small','Medium','Large','Extra Large'});
Y = [10 21 33 52];
bar(X,Y)
Categorías
Más información sobre Bar Plots en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

