# How to use the order of "x axis" in a graph of bar.

28 views (last 30 days)
Ana Soph on 12 Jul 2020
Commented: Ana Soph on 13 Jul 2020
Hi, thank you so much for your valious time, i have a problem, i want to do a bar graph, but always correct the order in x axis, re order the days, but i want to respect the order that i give to the vector, the vector have this order: but always reorder the days like that: well, right know this is my code:
clear;clc;
Dia = numData(:,2);
xlim([lenght(Dia)])
% xticks(Rad:5:length(Rad)) % 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.
% xlim([0:5:31])
perc = hline(9,'r', 'Días de cielo despejado y Parcialmente despejado'); hold on
perc = hline(5.5,'r', 'Días Parcialmente nublados'); hold on
perc = hline(4,'r', 'Días nublados'); hold on
I think is something so simple, but i try so many things i can't solve it
Thank you again
best!
Ana s.
##### 2 CommentsShow 1 older commentHide 1 older comment
Ana Soph on 13 Jul 2020
hi, sorry for not be clear, yes, something like that but respecting the order that I have in the vectors, so.. when i put the code that you give me, the order of "y" change, i mean the order of RAD in this case.
Thank you so much for answer me ...
best :)

Cris LaPierre on 12 Jul 2020
Edited: Cris LaPierre on 12 Jul 2020
You haven't specified an X-input in your bar plot command, so it's not reordering the data at all. Because you haven't specified X, it has. It assigns an index number to each value of Y: The first bar is given 1, the second, 2, etc.
You can change the xticklabels to be anything. By default it is the same as the tick location (x value) but can be something else (must be char or string). Note that, since your data is NOT in numeric order, you should specify a tick for every bar. It doen't make sense to have gaps.
xticks(1:length(Rad)) % 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.
xticklabels(string(Dia))
The other way is to specify the X-input. If you use Dia for X, it will reorder the bars to be in day order. That is the normal behavior for all plot types: numeric values are plotted in increasing order, and text data is in alphabetic order. You can't plot numeric data out of order. You would need to convert the datatype to categorical, and then specify the order of your categories. See this example from the bar plot documentation page.
1. Use categorical to convert
2. Use reordercats to specify the order
3. Then use bar, specifying x and y inputs.
dia = categorical(Dia);
dia = reordercats(dia,string(Dia)); Ana Soph on 13 Jul 2020
Thank you so much. And thank you for the recommendation, definitely i will check out every thing you said. I will hope you have a wonferfu Day. (: