How do I add monthly labels to my time series plot?

8 次查看(过去 30 天)
I have data points between 1 and 9862, respresenting each day from 1990 to 2017. I have extracted the first 5 years of data and want to add monthly labels on the x-axis e.g. Oct1990 Nov1990 ..... Sep1995.
  2 个评论
dpb
dpb 2023-1-24
9862/365.25
ans = 27.0007
Is only 27 years, while the difference between 2017 and 1970 is 47 or a 46 year span.
Hence the times cannot be daily.
Easiest would be to define what the time data point really are and use a @doc:datetime vector for the independent variable and plot against it instead. Then you get date labels "for free", the format for which you can set as desired.

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2023-1-25
Ah! Typos are the bane of programming! :)
Then
t=datetime(1990,1,1):datetime(2016,12,31); % datetime vector
is5=isbetween(t,t(1),t(1)+years(5)); % logical to get 5 years from beginning
plot(t(is5),randn(sum(is5),1)) % use your dependent variable indexed, too, of course
There's not enough space to label monthly unless you compress the formatting string and/or rotate the labels, but you get time labels automagically with the datetime axis.

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by