Put the name in the "x axis" of the graph bar every five days or seven days
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi, i hope all of you have a wonderful day, i have the matlab R2019a, and i want a simply thing, put the name of the days every five days or seven days to dont overlap the names of the days, because i can't see what day it is. I think is a very simple thing , but i can't find how to do it. i want to find the way to is easy to see the names
Thanks in advance..
Ana S
This is my code:
clear;clc;
numData = xlsread('enerosolar.xlsx','EneDic')
Dia = numData(:,1);
Rad = numData(:,2);
figure;
bar(Dia,'b')
xlabel('Día de Julio 2014')
ylabel('Irradiación Solar Global [KWh/m^2]');
xticks(1: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.
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(4.43,'r', 'Días parcialmente nublados'); hold on
0 Kommentare
Akzeptierte Antwort
dpb
am 12 Jul. 2020
Bearbeitet: dpb
am 12 Jul. 2020
Don't put the ticks every day; that's the cause for there being that many--you did it to yourself! :)
The bar() command will put ticks at convenient places automagically, if you don't like those you can then put them where want.
Use xticklabel to write something besides the tick value.
If you would convert to use datetime, you could get the day string for free...
ADDENDUM:
Sorry, got sidetracked and didn't get back when thought would/could...
It's relatively simple to use dates for the x axis and then the datetime ruler object gives you a lot of flexibility for dates --I'll also demonstrate the new(ish) MATLAB table object in lieu of the old xlsread route...
tEnersol=readtable('enerosolar.xlsx','Sheet','EneDic'); % read into a table object
tEnersol.Properties.VariableNames={'Rad','Dia'}; % assign variable names
yr=2014; % need the proper year
tEnersol.Date=datetime(yr,1,tEnersol.Dia); % and create a date column
hB=bar(tEnersol.Date,tEnersol.Rad); % bar plot against actual date
hAx=gca; % get axes handle...
xlim([datetime(2014,1,1) datetime(2015,1,1)]) % set limit to cover the full year
hAx.XAxis.TickLabelFormat='eee'; % show day of week
produced
which you'll note doesn't have the same shape as yours as it is plotted using the actual date in the data file on the x axis, not against ordinal position in the file (which is not ordered by date). This will show up if change the time format on the x-axis to 'MMM'
which illustrates the data must be from somewhere in the northern hemisphere.
6 Kommentare
dpb
am 13 Jul. 2020
Well, if you plot in sorted order, then the abscissa is as your original plot, simply the ordinal number of the observation.
BUT, then the axis isn't actually days at all; they've been lost by being ignored when you did the sort/plot.
So, labeling the x-axis with any day based on the number of the ordinal position would be completely happenstance to coincide with the actual day of that measurement.
If you have specific days to identify, then you'll have to pick those out specifically by looking up the actual day at that position and then you can label that point with that day's date.
That make sense?
Weitere Antworten (1)
dpb
am 13 Jul. 2020
Well, if you say so... :)
tEnersol=readtable('enerosolar.xlsx','Sheet','EneDic'); % read into a table object
tEnersol.Properties.VariableNames={'Rad','Dia'}; % assign variable names
yr=2014; % need the proper year
tEnersol.Date=datetime(yr,1,tEnersol.Dia); % and create a date column
tEnersol.Date.Format='MMMdd'; % display date format
hB=bar(tEnersol.Rad); % bar plot in descending order vs ordinal position
hAx=gca; % get axes handle...
xticks(1:14:365) % about all the ticks there's room for...
xticklabels(cellstr(tEnersol.Date(xticks))) % use the date string as tick labels
hAx.XTickLabelRotation=45; % rotate so can have chance to read...
If you have arbitrary locations to identify, it's the same idea; just have to look up what the index/ordinal number is of the wanted locations and put tick and ticklabel at those positions.
3 Kommentare
dpb
am 13 Jul. 2020
OK...as long as you realize what you're doing...that wasn't really clear initially that you were deliberately sorting the data and not plotting against the time vector on purpose.
The key simplification here again is the use of datetime to let it get you the date info and then the output comes essentially for free from picking the desired display format.
Just in this case need to remember to keep the indices in synch with the radiation data so if do anything to one column in the table to reorder must do same thing to all columns.
Siehe auch
Kategorien
Mehr zu Dates and Time finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!