How do I change the minor grid spacing from decimal to quarterly/monthly using datetick?
조회 수: 7 (최근 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!