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

11 visualizzazioni (ultimi 30 giorni)
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;
numData = xlsread('enerosolar.xlsx','EneDicS')
Rad = numData(:,1);
Dia = numData(:,2);
figure; bar(Rad,'b'), xlabel('Dia del año'), ylabel('Irradiación Solar Global [KWh/m^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 Commenti
jonas
jonas il 12 Lug 2020
Unclear what your desired result is. Do you want to plot the result in the order specified in Dia? Like
bar(Rad(Dia))
?
Ana Soph
Ana Soph il 13 Lug 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 :)

Accedi per commentare.

Risposta accettata

Cris LaPierre
Cris LaPierre il 12 Lug 2020
Modificato: Cris LaPierre il 12 Lug 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));
bar(dia,Rad,'b')
  4 Commenti
Cris LaPierre
Cris LaPierre il 13 Lug 2020
Modificato: Cris LaPierre il 13 Lug 2020
Your question didn't ask about hline. The function hline appears to be a file exchange function. You would need to reach out to the author for help, as that is not something MathWorks provides. However, perhaps try using the yline function instead.
You can adjust the xtick location and xtick labels however you want at this point. However, knowing what the x-values are, I think you'd almost be better not labeling them at all. Incrementing is only helpful when one can easily determine what the missing values are. Here, even your ticks will be out of order, which is thoroughly confusing.
Anyway, something like this should work (I didn't test it).
xticks(1:5:length(Rad));
xticklabels(string(Dia(1:5:end)));
Ana Soph
Ana Soph il 13 Lug 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. (:

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by