how to set a frequency of x axis of a plot?
27 ビュー (過去 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
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!