Azzera filtri
Azzera filtri

Matlab Bar Graph Lables In Alphabetical Order

7 visualizzazioni (ultimi 30 giorni)
Bryan Seefeld
Bryan Seefeld il 25 Lug 2017
Risposto: Cam Salzberger il 25 Lug 2017
I am trying to show a number of a product by month in a bar graph. The issue is that when I plot the data, it puts it in Alphabetical order as opposed to the order I put it in. Here is the code
category=categorical({'Jan17','Feb17','March17','April17','May17','June17','July17','Aug17','Sept17','Oct17','Nov17','Dec17','Jan18','Feb18','March18','April18','May18','June18','July18','Aug18','Sept18','Oct18','Nov18','Dec18','Jan19','Feb19','March19','April19','May19','June19','July19','Aug19','Sept19','Oct19','Nov19','Dec19','Jan20','Feb20','March20','April20','May20','June20','July20','Aug20','Sept20','Oct20','Nov20','Dec20'});
graph=[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,y,a1,b2,c3,d4,e5,f6,g7,h8,i9,j10,k11,l12,m13,n14,o15,p16,q17,r18,s19,t20,u21,v22,w23,y24];
figure; bar(category,graph)
%% The variables in the graph array are already assigned number values

Risposte (2)

jpai
jpai il 25 Lug 2017
Modificato: jpai il 25 Lug 2017
What you can do to achieve your desired outcome is to apply some custom properties to the labels on the x-axis. For your case, you can use the following code to obtain the custom labels and ordering that you desire. You just need to separately apply the x-axis labels.
category={'Jan17','Feb17','March17','April17','May17','June17','July17','Aug17','Sept17','Oct17','Nov17','Dec17','Jan18','Feb18','March18','April18','May18','June18','July18','Aug18','Sept18','Oct18','Nov18','Dec18','Jan19','Feb19','March19','April19','May19','June19','July19','Aug19','Sept19','Oct19','Nov19','Dec19','Jan20','Feb20','March20','April20','May20','June20','July20','Aug20','Sept20','Oct20','Nov20','Dec20'};
graph = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,y,a1,b2,c3,d4,e5,f6,g7,h8,i9,j10,k11,l12,m13,n14,o15,p16,q17,r18,s19,t20,u21,v22,w23,y24];
figure; bar(graph)
xticklabels(category) % This will set labels to be used for each tick of the x-axis
xticks(1:1:length(category)) % This will set how many ticks you want on the x-axis. Here, there should be 48 ticks being generated. One for each piece of data you have.
xtickangle(90) % This will rotate the label so that the labels will not overlap with one another. This is in degrees.
What is happening here is the graph is being generated with your y-axis data in the order of the values in the matrix graph. Then, the x-axis properties are set using the xticklabels, xticks, and xtickangle functions.
For more information regarding how you can adjust graph elements after the graph is generated you can visit the following link: https://www.mathworks.com/help/matlab/creating_plots/change-tick-marks-and-tick-labels-of-graph-1.html
Hope this helps!

Cam Salzberger
Cam Salzberger il 25 Lug 2017
Hey Bryan,
The key difficulty here is that categorical data, if not ordinal, has no inherent order. So it be presented in a different order than it went in is not incorrect. If you were to turn this into ordinal data, then MATLAB would sort it based on the < and > operators. If you aren't using a custom class to override those, you'll get alphabetical ordering anyway.
jpai has a good answer for how you can get this graph to easily look the way you want it to. Here's one other way that may address making the graph represent the data in a meaningful way.
If you turn your x-data into datetime values, since they do actually represent dates, you can preserve the data in the XData itself, rather than the labels. This also gives you more flexibility for messing around with the data, and is inherently ordered in the way you would expect it to be.
x = datetime({'Jan17','Feb17','Mar17'},'InputFormat','MMMyy','Format','MMMyy');
y = 100*rand(size(x));
bar(x,y)
The 'InputFormat' tells "datetime" how to read the data. The 'Format' tells it how to display the data (such as when shown on tick labels). See the datetime documentation for more information.
One issue though is that your date format isn't cohesive. Some months have four or five letters, while others have only three. If you fix that up though, then this will work for your full data:
x = datetime({'Jan17','Feb17','Mar17','Apr17','May17','Jun17','Jul17','Aug17','Sep17','Oct17','Nov17','Dec17','Jan18','Feb18','Mar18','Apr18','May18','Jun18','Jul18','Aug18','Sep18','Oct18','Nov18','Dec18','Jan19','Feb19','Mar19','Apr19','May19','Jun19','Jul19','Aug19','Sep19','Oct19','Nov19','Dec19','Jan20','Feb20','Mar20','Apr20','May20','Jun20','Jul20','Aug20','Sep20','Oct20','Nov20','Dec20'},'InputFormat','MMMyy','Format','MMMyy');
y = 100*rand(size(x));
bar(x,y)
xticks(x)
xtickangle(90)
-Cam

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by