How do I change the minor grid spacing from decimal to quarterly/monthly using datetick?
5 ビュー (過去 30 日間)
古いコメントを表示
I am plotting production vs. time and when I turn on the minor grid lines MATLAB creates 10 divisions between the years (major grid lines). I would like to either have 4 minor grid lines (quarterly) or 12 minor grid lines (monthly), which is more intuitive. I tried subdividing the grid lines using datenumber, but once I use datetick on the x-axis it changes the placement. I have looked all over the documentation and answer forums but haven't seen anything about this. Is there a built in solution for this? If not, what could be a possible workaround?
0 件のコメント
採用された回答
Star Strider
2016 年 8 月 30 日
The datetick function ignores individual ticks specified with the set function. You can create a workaround with a text object and a bit of code.
Example:
CreateMonths = @(StartVec,N) datenum(bsxfun(@plus, StartVec, [zeros(N,1) [0:N-1]' zeros(N,1)]));
DV = CreateMonths([2013 06 01],37); % Create Data
Y = randi([1000 12000], size(DV)); % Create Data
figure(1)
plot(DV, Y)
grid
Qrtrs = DV(1:3:end);
set(gca, 'XTick', Qrtrs, 'XTickLabel',[]) % Set ‘XTicks’, Turn Off Automatic Labels
ds = datestr(Qrtrs, 'yy QQ');
text(Qrtrs, -100*ones(size(Qrtrs)), ds, 'Rotation',30, 'HorizontalAlignment','right', 'VerticalAlignment','top', 'FontSize',8)
The ‘CreateMonths’ function is just a little utility I wrote to create date vectors. I’m including it here so you can run my example code. I would use quarters rather than months, because otherwise the x-axis gets too crowded.
Experiment with this to get the result you want with your data.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Dates and Time についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!