Changing number of dates shown on xaxis

2 Ansichten (letzte 30 Tage)
Locks
Locks am 7 Sep. 2015
Bearbeitet: dpb am 7 Sep. 2015
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

Akzeptierte Antwort

dpb
dpb am 7 Sep. 2015
Bearbeitet: dpb am 7 Sep. 2015
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 Kommentar
Locks
Locks am 7 Sep. 2015
perfect, excatly what I needed, thanks a lot both of you!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by