MATLAB Answers

Time series plot: put YYYYMM on x-axis?

25 views (last 30 days)
Christoph Meier
Christoph Meier on 11 Aug 2015
Commented: Christoph Meier on 12 Aug 2015
Dear Matlab community,
in advance: Many thanks for the great help I always get!
I am trying to plot a time series (in a column), with the simple following function:
plot(timeseries);
The result is exactly as I hoped for:
However, I am trying to put dates on the x-axis. I have a separate column variable for that with the same dimensions, called 'ffdates': 192601 192602 192603 192604 192605 192606 192607 192608 192609 192610 192611 192612 192701 192702 ...
I would like to display those on the x-axis, but if I execute the respective command
plot(ffdates,timeseries);
i get this:
I suspect that the distortion comes from the date jumps 192612 to 192701. However, is there a way that I can simple use the variable 'ffdates' as x-axis labels?
Alternatively, I would also be happy with defining a date range from 07/1926 to 12/2014 to the x axis, in any date format.
Thank you very much in advance!
Chris

  0 Comments

Sign in to comment.

Accepted Answer

dpb
dpb on 11 Aug 2015
>> dn=datenum(1926,7+[0:(2014-1926)*12+5].',1);
>> datestr([dn(1));dn(end)])
ans =
01-Jul-1926
01-Dec-2014
>> y=rand(size(dn)); % some dummy data
>> plot(dn,y)
>> xlim([dn(1) dn(end)])
>> datetick('x','mm/yyyy','keeplimits')
>>
Adjust ticks but won't have sufficient room to label every month by any stretch...

  3 Comments

Christoph Meier
Christoph Meier on 11 Aug 2015
Thank you so much! It works flawlessly.
I only came across a minor issue. I replaced ';' with ',' in line datestr([dn(1), dn(end)]); As below:
dn=datenum(1926,7+[0:(2014-1926)*12+5].',1);
datestr([dn(1), dn(end)]);
plot(dn,y);
xlim([dn(1) dn(end)]);
datetick('x','mm/yyyy','keeplimits');
dpb
dpb on 12 Aug 2015
No problem, glad to help...on the datestr call, that's totally immaterial; I stuck it in there simply to demonstrate what the two start/end dates generated above were...you don't need it at all.
Christoph Meier
Christoph Meier on 12 Aug 2015
I see! Always learning. Contributors like you are incredibly valuable to rookies like me :) Thanks again!

Sign in to comment.

More Answers (0)