Put the name in the "x axis" of the graph bar every five days or seven days

2 次查看(过去 30 天)
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

采纳的回答

dpb
dpb 2020-7-12
编辑:dpb 2020-7-12
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 个评论
dpb
dpb 2020-7-13
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?
Ana Soph
Ana Soph 2020-7-13
well, yes is make sense but i have something like that, but i want in the x axis the real name of the days every five days, like this (the first photo) but this is make it in excel.
My code right know is like this
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]');
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

请先登录,再进行评论。

更多回答(1 个)

dpb
dpb 2020-7-13
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 个评论
dpb
dpb 2020-7-13
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.
Ana Soph
Ana Soph 2020-7-13
编辑:Ana Soph 2020-7-13
Oh. I know. I wasn't being clear and i will remember your instructions, again thank you for you patience and for explain to me every thing. This was so educative.i hope you have an amazing Day. Best!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by