Effacer les filtres
Effacer les filtres

How to add minor ticks to a time series

114 vues (au cours des 30 derniers jours)
Candice Cooper
Candice Cooper le 10 Août 2021
Commenté : Candice Cooper le 11 Août 2021
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!

Réponse acceptée

Dave B
Dave B le 10 Août 2021
Modifié(e) : Dave B le 10 Août 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);
  1 commentaire
Candice Cooper
Candice Cooper le 11 Août 2021
thank you this was very helpful!!!!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Calendar dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by