Problem with x-axis and dates in plot
6 ビュー (過去 30 日間)
古いコメントを表示
Hi everybody,
I want to plot 228 datapoints, which correspond to monthly observations from 01/1993 to 12/2011.
The x-axis of the plot should be in 'yyyy' format, showing the first year (1993), the last year (2011) and maybe three or four equally spaced dates in between those years.
How can I do that?
Thanks in advance, Chris
0 件のコメント
回答 (2 件)
Kelly Kearney
2013 年 11 月 7 日
If you're not too picky about exactly which years are labeled, datetick should do it. Assuming t holds your time data (as datenumbers):
y = rand(228,1);
[month, yr] = ndgrid(1:12, 1993:2011);
t = datenum(yr(:), month(:), ones(numel(yr),1));
plot(t,y);
datetick('x', 'yyyy');
If you want to be more specific about exactly which years are labeled, set the xticks manually before calling datetick, and then use the 'keepticks' and 'keeplimits' options.
0 件のコメント
Christian F.
2013 年 11 月 8 日
1 件のコメント
Kelly Kearney
2013 年 11 月 8 日
So set your preferred axis limits, and then use 'keeplimits' to make datetick respect this:
y = rand(228,1);
[month, yr] = ndgrid(1:12, 1993:2011);
t = datenum(yr(:), month(:), ones(numel(yr),1));
plot(t,y);
set(gca, 'xlim', [min(t) max(t)]);
datetick('x', 'yyyy', 'keeplimits');
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!