how to set a frequency of x axis of a plot?

30 views (last 30 days)
I have a plot
plot(yyyyMM,temperature)
This plot has x axis that shows year. But I want to have more fine ticks such as quaterly, monthly, etc. How can one set such frequency?

Accepted Answer

dpb
dpb on 5 Jul 2020
If your variable is (as was suggested in other thread asking about formatting it) a datetime, just use xticks with the time span you want (as datetimes/durations).

More Answers (1)

dpb
dpb on 5 Jul 2020
Edited: dpb on 5 Jul 2020
Read up on datetime and plot and the DatetimeRuler...
t=datetime(2018:2033,1,1); y=randn(size(t)); % dummy data over your time frame
plot(t,y)
xl=xlim; % get the axis limits array
xt=xl(1)+calmonths(0:6:(2032-2018+1)*6*2); % compute ticks on 6 calendar month cycle
xticks(xt) % and set on graph
hAx=gca; % get axes handle
hAx.XAxis.TickLabelRotation=45; % rotate the labels 45 degrees
Above yields
As you'll observe, date tick labels take up a lot of room; you'll have to stretch out the axis, make the font smaller, shorten the format chosen or just use fewer of 'em, but that's the basics.
It's no problem if use the right data class.
hAx.XAxis.TickLabelInterpreter='none';
hAx.XAxis.TickLabelFormat='yyyy_MM';
will set the format string to display the yyyy_MM string which is a little less verbose -- the underscore gets interpreted as subscript for subsequent character w/ default 'TeX' interpreter; hence the 'none'

Tags

Community Treasure Hunt

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

Start Hunting!