MATLAB Answers

Plot year as major ticks and months as minor ticks

3 views (last 30 days)
Chris McGibbon
Chris McGibbon on 16 Jun 2021
Commented: Chris McGibbon on 18 Jun 2021
I haves several years worth of time series data and I want to plot the major ticks as years, with labels and mior ticks as months with no labels. I can see how to plot major and minor ticks, but not with date/time data.

Accepted Answer

dpb on 16 Jun 2021
If your time data are evenly-spaced and at monthly intervals,
hAx=gca; hX=hAx.XAxis; % get handle to X datetime ruler object
xt=hX.TickValues; % get the tick values; presuming are yearly by default
isYr=(mod(1:numel(xt),12)==1); % the indices to the year tick locations
hX.MinorTickValues=xt(~isYr); % write the monthly dates as minor ticks
If your data aren't regular or don't begin precisely on the first of the year, then use create a regular monthly timeseries between the XLIM values of the axis and write those similarly.
It does seem as though the datetimeRuler could/should have a 'monthly' and various other named intervals to use to specify the common labeling that one would want similar to the options in 'retime'
  1 Comment
Chris McGibbon
Chris McGibbon on 18 Jun 2021
The data are neither evenly spaced, nor at monthly intervals, but I'll try making the monthly time series segments.
Thanks for the help.

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!

Translated by