Azzera filtri
Azzera filtri

How to add minor ticks to a time series

114 visualizzazioni (ultimi 30 giorni)
I have a time series spanning 24 years. The dates are in matlab datenum format, and I am using
datetick('x','yyyy')
to add the dates to the x axis. This puts the years as major ticks. I want to add minor ticks for each month within each year, but am struggling to figure out how to do so. Would it be possible to just specify the number of minor ticks between each major tick? Any help is appreciated! Thanks!

Risposta accettata

Dave B
Dave B il 10 Ago 2021
Modificato: Dave B il 10 Ago 2021
You can control the minor tick spacing with the MinorTickValues property with the XAxis property on the Axes...
% some fake data
x = datenum(datetime(2001:2024,1,1));
y=rand(24,1);
plot(x,y)
datetick('x','yyyy')
set(gca,'XMinorTick','on')
xl = xlim;
xAx = get(gca,'XAxis');
xAx.MinorTickValues=linspace(xl(1),xl(2),288);
Obviously it's indistinguishable, but worth noting these aren't months they're 12ths of years...
Things are better with datetime than datenum, although the result looks the same it makes it a little easier to think/code about date units (and the ticks are now at actual month beginnings, as crazy as our calendar is!):
x=datetime(2001:2024,1,1);
plot(x,y)
xlim([datetime([2000 2024],1,1)])
xticks(datetime(2000:2:2024,1,1))
xtickformat('yyyy')
set(gca,'XMinorTick','on')
xAx=get(gca,'XAxis');
xAx.TickLabelRotation=30;
xl=xlim;
xAx.MinorTickValues = xl(1):calmonths(1):xl(2);

Più risposte (0)

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by