How to split x-axis (6 years of timeseries) with datetick into monthly intervals?

6 次查看(过去 30 天)
Hello,
I've got my time series data of 6 years plus and I would like to plot them in that way so that in the x-axis I would have the year and the month.
I tried the code below, but it gives me strange result, for example some months are skipped, the spacing does not seem to be equal sized.
What do you think I could do?
Thanks a lot!
%%
startdate = datenum('20-Oct-2002 15:00:00');
enddate = datenum('28-Dec-2008 11:00:00');
t = linspace (startdate,enddate,54261); %54261 is the length of my dataset
figure
plot(t,Rain)
grid on
NumTicks = 72;
L = get(gca,'XLim');
set(gca,'XTick',linspace(L(1),L(2),NumTicks))
datetick('x','mmm yyyy', 'keepticks')
set(gca,'XMinorTick','on') %,'YMinorTick','on')
rotateticklabel(gca, 90);

回答(1 个)

dpb
dpb 2013-11-1
编辑:dpb 2013-11-1
...
set(gca,'XTick',linspace(L(1),L(2),NumTicks))
...
Unless L and NumTicks are some very particular sets of values, the resulting xTick values are going to be noninteger and this can't be good. Given the number and the overall scale I suspect (altho I haven't calculated it) the magnitude of the difference in rounding is large enough to be observable.
You'll do better using datenum with a monthly increment vector from first of first month to first of last+1 in the dataset and the internal ability in datenum to handle the rollover internally to set the tick mark positions. Then you'll get the precise date numbers for the positions accounting for the varying number of days/month, leap year, etc., etc., ...
ADDENDUM:
ie, sotoo
tt=datenum(2002,[1:72]',1,0,0,0); % tick times monthly from 1/1/2002
set(gca,'xtick',tt)
Now use datetick to format the labels as desired.
You may want to start at the month after the first month of data and use one less to keep within the limits of the data on the axes or set the limits at the beginning of the first month and first of succeeding month of last included in the data--that's all depending on how you want the plot to appear.

类别

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