How do I change the minor grid spacing from decimal to quarterly/monthly using datetick?

6 次查看(过去 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?

采纳的回答

Star Strider
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 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by