How to add minor ticks to a time series

123 次查看(过去 30 天)
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!

采纳的回答

Dave B
Dave B 2021-8-10
编辑:Dave B 2021-8-10
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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Calendar 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by