how to set a frequency of x axis of a plot?
21 次查看(过去 30 天)
显示 更早的评论
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?
2 个评论
采纳的回答
dpb
2020-7-5
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).
更多回答(1 个)
dpb
2020-7-5
编辑:dpb
2020-7-5
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/327449/image.jpeg)
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'
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!