Need help regarding axis labeling using datetick

2 views (last 30 days)
I want to plot a time series which has 322 data points. First point is Jan1991, second is Feb1991........so on. last is Oct2017 (each with a months gap) But i only want ticks at the interval of 6 months.
So far after using plot, I am doing the following:
startDate = datenum('01-01-1991');
endDate = datenum('31-10-2017');
endDate = datenum('10-31-2017');
xData = linspace(startDate,endDate,25);
ax = gca;
ax.XTick = xData;
datetick('x','mmmyy','keepticks')
But the problem is that my old plot is gone and I am only left with ticks. I tried using 'hold on' but nothing happened. Any suggestions?
  6 Comments
Lavnish Gupta
Lavnish Gupta on 1 Jul 2018
I am having defined variable for time which is [1 2 3 4....322]. Similarly, I am having my other defined variables A,B,C,D which all have same size as time, 322 X 1.
I am using
plot(time,A,time,B,time,C,time,D);
I want the date ticks for this plot as specified earlier for which I am using (immediately after plot):
startDate = datenum('01-01-1991');
endDate = datenum('31-10-2017');
endDate = datenum('10-31-2017');
xData = linspace(startDate,endDate,25);
ax = gca;
ax.XTick = xData;
datetick('x','mmmyy','keepticks')
dpb
dpb on 1 Jul 2018
Edited: dpb on 1 Jul 2018
Then you MUST plot the A,B,C,C vectors against the appropriate time axis as well.
As recommended earlier, use datetime instead of datenum and things will be MUCH simpler as the plot axes are aware of it and you can dispense with datetick entirely.
As it is now, you have one axes on which to simultaneously plot data which run from [1 322] and another dataset that is the datenum value for 1/1/1991 which is ...well, let's see--
>> disp([datenum(1991,1,1) datenum(2017,10,31)])
727199 736999
>>
so to show those on the axis the previous data are way the heck over in left field at
>> disp([datestr(1) ' ' datestr(322)])
01-Jan-0000 17-Nov-0000
>>
which is the arbitrary start point for datenum.
You COULD use two separate axes, but that's even more complexity; the correct solution is to use the new(ish) datetime object instead and leave the obsolete datenum behind. (Alternatively, you might try storing the actual datestrings into the timeseries object and see what happens with its specific plot routine (presuming it has one); I've never tried it; what little I've done w/ it so far it has proved to be more of a hindrance than help.
Let's see, there are 9800 days between those two dates, for 322 observations that works out to rough 30 days...so do you have monthly observations; are they collected on same day of month or do you otherwise know the actual data dates? That would be what to use if known.
ADDENDUM
Ah! I see you answered the question in the beginning...we got sidetracked in the details. With this, see Answer...

Sign in to comment.

Accepted Answer

dpb
dpb on 1 Jul 2018
Edited: dpb on 2 Jul 2018
>> dt=datetime(1991,1+[0:321].',1); % build a datetime vector to match the dataset
>> whos dt % see we got what wanted...are 322 values!
Name Size Bytes Class Attributes
dt 322x1 5153 datetime
>> [dt(1) dt(end)] % and they match expected dates, besides! :)
ans =
1×2 datetime array
01-Jan-1991 01-Oct-2017
>>
Once you have that, then simply plot your data against them...
hL=plot(dt,[A B C D]); % plot versus the time vector; variables are column vectors
It is much simpler in Matlab if you do NOT create sequentially-named/numbered variables but use array syntax and subscripting to refer to individual columns (variables) within.
With the above, ML will automatically place ticks on a scale that will fit; probably only annually given the amount of data you have and the room required for date ticks to be drawn. You can change these arbitrarily with the DatetimeRuler properties;
>> hAx=gca; % get the axes handle
>> hAx.XAxis.TickLabelFormat % see what is default...as thought, just years
ans =
'u'
>> hAx.XAxis.TickLabelFormat='MMMyy'; % set it to display JanYY for each year instead
You can set the number and spacing of ticks as please, with using datetime you can do this with direct datetime values and not have to mess with datetick at all; MUCH more convenient!
>> xlim % what are the default limits...
ans =
1×2 datetime array
Jan 01, 1991 Oct 01, 2017
>>
To set six-months ticks, just select every sixth time value that you already have--
hAx.XTick=dt(1:6:end); % six-months tick intervals
Those are undoubtedly going to overlap; try to see if can fit...
hAx.XTickLabelRotation=30; % rotate
hAx.FontSize=8; % make smaller font
Salt to suit from these to get what want...
  2 Comments
Lavnish Gupta
Lavnish Gupta on 2 Jul 2018
@dpb
Thank you so much! It worked perfectly as required. :)
dpb
dpb on 2 Jul 2018
You're welcome...using datetime has many advantages for such things over classic datenum; datetick has many warts/idiosyncracies that have been significantly cleaned up and TMW continues to make the integration more complete. Not sure every one of the specialized plots is yet datetime-aware, but the major ones are now after some early growing pains.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!