Why are minor ticks not being added to my graph?

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) ;

댓글 수: 1

My photo didn't seem to upload above, so there it is:

댓글을 달려면 로그인하십시오.

 채택된 답변

Voss
Voss 2025년 3월 10일
편집: Voss 2025년 3월 10일
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'})
G = 7x2 table
Date Avg_ESDiamiter ___________ ______________ 10-Mar-2024 9 11-Mar-2024 9 12-Mar-2024 1 13-Mar-2024 7 14-Mar-2024 6 15-Mar-2024 1 16-Mar-2024 7
% 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(:)
ans = 13x1 datetime array
10-Mar-2024 00:00:00 10-Mar-2024 12:00:00 11-Mar-2024 00:00:00 11-Mar-2024 12:00:00 12-Mar-2024 00:00:00 12-Mar-2024 12:00:00 13-Mar-2024 00:00:00 13-Mar-2024 12:00:00 14-Mar-2024 00:00:00 14-Mar-2024 12:00:00 15-Mar-2024 00:00:00 15-Mar-2024 12:00:00 16-Mar-2024 00:00:00
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

Thanks, your suggestions works great. But I don't understand some of the syntax. In the following lines of code:
mtv = interp1(1:NG,G.Date,linspace(1,NG,2*NG-1)).'
what does the period and apostraphy ( .' ) mean/do at the end of that line?
The .' is (regular) transpose (as opposed to conjugate transpose, which is the ' operator by itself with no period)
linspace() creates a row vector, so interp1() would respond with a row vector. The .' afterwards would change the row vector into a column vector.
It looks to me as if likely the assignment to ax1.XAxis.MinorTickValues would automatically repair row versus column vector, so I am not presently convinced that the transpose is necessary.
dpb
dpb 2025년 3월 10일
See transpose, .' Set a break point at the line and look at the result with/without the transpose.
Nota Bene: the difference between it and ctranspose, ':
Voss
Voss 2025년 3월 10일
Walter is right, the transpose is not necessary; I included it to make mtv show more nicely on the command line. I've edited the answer.
Thank you!!
Voss
Voss 2025년 3월 10일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

dpb
dpb 2025년 3월 10일
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...

댓글 수: 1

Hi, are you saying
hAx.XAxis.TickLabels=hAx.XAxis.TickLabels;
is code that removes the "2024" lable under the x-axis labels?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

제품

릴리스

R2024b

질문:

2025년 3월 10일

댓글:

2025년 3월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by