フィルターのクリア

How to add minor ticks to a time series

127 ビュー (過去 30 日間)
Candice Cooper
Candice Cooper 2021 年 8 月 10 日
コメント済み: Candice Cooper 2021 年 8 月 11 日
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);
  1 件のコメント
Candice Cooper
Candice Cooper 2021 年 8 月 11 日
thank you this was very helpful!!!!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCalendar についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by