Need help regarding axis labeling using datetick
Show older comments
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
There's no data plotted at any point in that code. So it's not that your plot is gone, your plot was never really produced in the first place. Clear your workspace and execute each line consecutively, one by one, and you'll see that there was never data plotted to begin with (unless there's more to the code than what you shared).
dpb
on 29 Jun 2018
First suggestion presuming you have release R2016b or later would be to switch from datenum to datetime; then plot is enhanced to use the native class instead so don't need datetick
Your date span above is some 26 years, so there would be over 50 tick marks at six-month intervals; that's way too many to have room to display on the axis; even the above with 30 degree rotation is quite cluttered.
As for the other plot data apparently being gone, did you plot it against the same range of datenum values? The data and the axis have to have the same range of xlim values.
Lavnish Gupta
on 1 Jul 2018
dpb
on 1 Jul 2018
Doesn't help to diagnose without data nor specific code...we can't see what you did so have no klew. By "timeseries" do you mean the builtin Matlab object or just the generic description of what the data are? If it is, the "time" inside that collection is either just a relative time from an origin of 0 as doubles or absolute stored as date strings; the ranges will not overlap with a datenum value of the same calendar date.
Lavnish Gupta
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...
Accepted Answer
More Answers (0)
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!