MATLAB Answers

Changing number of dates shown on xaxis

1 ビュー (過去 30 日間)
Locks
Locks 2015 年 9 月 7 日
編集済み: dpb 2015 年 9 月 7 日
Hi, I have a plot with time series data of roughly 2 years. When I plot the series, I insert datetick('x','mmmyyyy'); to Format the numerical data. The issue is that I only get displayed two ticks (Jan 2000 and Jan 2010) however I would like to see a tick for each 5 year period for example. The documentation of datetick was not really helpful for this, is there an easy way to define how many ticks including the date gets displayed? thx

採用された回答

dpb
dpb 2015 年 9 月 7 日
編集済み: dpb 2015 年 9 月 7 日
I had presumed the '2' was intended as '20' but had to go to lunch so Star beat my example :) But, the following that had prepared excepting for a typo should resolve the question raised in the follow up--
dn=datenum([1993:2016],1,1).'; % a set of datenums over some 20+ yr
y=rand(size(dn)); % and some dummy data to go along...
plot(dn,y)
yr=datevec([dn(1) dn(end)]); yr=yr(:,1) % get the actual year range
dt=datenum([yr(1)-mod(yr(1),5):5:yr(end)],1,1); % years modulo 5
set(gca,'xtick',dt) % and set the tick marks at those dates
xlim([dn(1) dn(end)]) % now set x limits to range of x dates
datetick('x','mmmmyyyy','keeplimits','keepticks') %to date display
NB: 'keeplimits','keepticks' will prevent the automagic prettifier from futzing around with the values you've asked for. The full month may turn out to be too long for as many ticks as you've asked for; the 'mmmyyyy' format may work better, but you'll have to judge how it looks...
  1 件のコメント
Locks
Locks 2015 年 9 月 7 日
perfect, excatly what I needed, thanks a lot both of you!

サインインしてコメントする。

その他の回答 (0 件)

製品

Community Treasure Hunt

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

Start Hunting!

Translated by