Trying to add Minor Ticks and grid with duration data
24 views (last 30 days)
Show older comments
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';
I don't understand what I am doing wrong. Please help, thanks.
0 Comments
Answers (1)
ANKUR KUMAR
on 27 Jan 2022
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
See Also
Categories
Find more on Annotations 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!