Why are minor ticks not being added to my graph?
13 views (last 30 days)
Show older comments
I used this resource to find how to add minor ticks: https://www.mathworks.com/help/matlab/ref/matlab.graphics.axis.decorator.numericruler-properties.html
I added it to my code below but it does not show up on my graph as seen in the picture (found under comments):
figure (1)
scatter(G.Date, G.("Avg_ESDiamiter"), 'LineWidth',1 ) ;
ax1 = gca ;
ax1.LineWidth = 1.5 ;
ax1.XAxis.MinorTick = 'on' ; % Here is where I added minor ticks
fontsize(gca, 12,'points') ;
xtickformat("MM-dd") ;
xtickangle(315) ;
Accepted Answer
Voss
on 10 Mar 2025
Edited: Voss
on 10 Mar 2025
To get minor ticks on a datetime axis: In addition to setting the axis ruler's MinorTick property 'on', apparently you have to specify the minor tick values themselves by setting the axis ruler's MinorTickValues property.
Example:
G = table(datetime(2024,3,(10:16).',0,0,0),randi(10,7,1),'VariableNames',{'Date','Avg_ESDiamiter'})
% example minor tick values (interpolate a new value
% between each consecutive pair of G.Date values):
NG = height(G);
mtv = interp1(1:NG,G.Date,linspace(1,NG,2*NG-1));
mtv(:)
if true
figure (1)
scatter(G.Date, G.("Avg_ESDiamiter"), 'LineWidth',1 ) ;
ax1 = gca ;
ax1.LineWidth = 1.5 ;
ax1.XAxis.MinorTick = 'on' ; % Here is where I added minor ticks
ax1.XAxis.MinorTickValues = mtv; % set the minor tick values
fontsize(gca, 12,'points') ;
xtickformat("MM-dd") ;
xtickangle(315) ;
end
6 Comments
More Answers (1)
dpb
on 10 Mar 2025
d=datetime(2024,6,3+[0:35],'format','MM-dd'); % datetimes by day
scatter(d,rand(size(d)))
hAx=gca;
hAx.XAxis.MinorTickValues=d; % daily minor ticks
hAx.XAxis.MinorTick='on'; % and display...
box on
% this is an as far as I can tell, undocumented trick to turn off the secondary "year" label
hAx.XAxis.TickLabels=hAx.XAxis.TickLabels;
Have to both turn them on and give values...
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!