# Trying to add Minor Ticks and grid with duration data

조회 수: 4(최근 30일)
Kyle Cox 2022년 1월 27일
편집: Kyle Cox 2022년 1월 27일
I am trying to add minor ticks and grids to some data I am plotting. I have created a simple example below. When I use duration, I don't get any Minor Ticks or Grid but if I plot x_seconds directly then the Minor Ticks and Grid works just fine. For my application, the time format given by duration is much prefered for the plot vs the raw seconds. I mainly use R2017b but same thing happens in R2018b.
Edit: I realize now that I have been using my plots mostly for data exploration and that having the grid and ticks set automatically such as when I zoom is important. I may have to give up using duration on my plots.
% simple data example
x_seconds = 5000:0.3:5500;
x_duration = duration(0,0,x_seconds);
y = 1:length(x_seconds);
% Using duration
figure
plot(x_duration,y)
ax1 = gca();
ax1.XAxis.MinorTick = 'on';
ax1.XMinorGrid = 'on'; % using raw seconds
figure
plot(x_seconds,y)
ax2 = gca();
ax2.XAxis.MinorTick = 'on';
ax2.XMinorGrid = 'on'; 댓글을 달려면 로그인하십시오.

### 답변(1개)

ANKUR KUMAR 2022년 1월 27일
If you want grid line and ticks both in the plot, you can prefer handling ticks direclty on the majorticks. Here is an example of that. You just have to edit the xticks as per your requirement.
Instead of depending of MATLAB to auto put the xticks, I would recommend you to specifiy ticks on your own in the xticks.
x_seconds = 5000:0.3:5500;
x_duration = duration(0,0,x_seconds);
y = 1:length(x_seconds);
% Using duration
figure
plot(x_duration,y)
xticks(x_duration(1:250:end))
grid on
set(gca,'GridLineStyle',':') figure
x_seconds = 5000:0.3:5500;
x_duration = duration(0,0,x_seconds);
y = 1:length(x_seconds);
% Using duration
for i = 1:4
subplot(2,2, i)
plot(x_duration,y)
xticks(x_duration(1:150*i:end))
grid on
set(gca,'GridLineStyle',':')
xtickangle(45)
end ##### 댓글 수: 1표시숨기기 없음
Kyle Cox 2022년 1월 27일
That is a pretty good option, thanks. Although, I am realizing now that I have mostly been using my plots for data exploration so having the ticks and grid lines automatically change is the feature I really want. I may have to settle for plotting against raw seconds.

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

### 범주

Find more on Axis Labels in Help Center and File Exchange

R2017b

### Community Treasure Hunt

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

Start Hunting!