How to use the order of "x axis" in a graph of bar.
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ana Soph
el 12 de Jul. de 2020
Comentada: Ana Soph
el 13 de Jul. de 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;
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 comentarios
jonas
el 12 de Jul. de 2020
Unclear what your desired result is. Do you want to plot the result in the order specified in Dia? Like
bar(Rad(Dia))
?
Respuesta aceptada
Cris LaPierre
el 12 de Jul. de 2020
Editada: Cris LaPierre
el 12 de Jul. de 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.
- Use categorical to convert
- Use reordercats to specify the order
- Then use bar, specifying x and y inputs.
dia = categorical(Dia);
dia = reordercats(dia,string(Dia));
bar(dia,Rad,'b')
4 comentarios
Cris LaPierre
el 13 de Jul. de 2020
Editada: Cris LaPierre
el 13 de Jul. de 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)));
Más respuestas (0)
Ver también
Categorías
Más información sobre Annotations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!