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개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by