Clear Filters
Clear Filters

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

16 views (last 30 days)
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 Comments
dpb
dpb on 24 Jan 2023
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.

Sign in to comment.

Answers (1)

dpb
dpb on 25 Jan 2023
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.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!