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

6 Ansichten (letzte 30 Tage)
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?

Akzeptierte Antwort

Star Strider
Star Strider am 30 Aug. 2016
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.

Weitere Antworten (0)

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by