# how to set xticks and xline

8 views (last 30 days)
OMAR MOUSA on 29 Jun 2023
Commented: Rik on 29 Jun 2023
I would like to set xticks and and xline in the figure but seems diffcult to set them
I have a data with 1 second every sample for 6 days so the number of samples is 6 days*24 hours*60 minutes*60 seconds = 518400
how to make x ticks at every 12 hours and xline every 24 hours? I use this code below to plot the figure
[minA,maxA] = bounds(Pc5); % The maximum and minimum values of aggregate data
plot(t,Pc1,'r',t,Pc2,'g',t,Pc3,'m',t,Pc4,'b',t,Pc5,'y',t,Pc6,'c',t,Pc7,'k',t,Pmain,'k');
xticks(linspace(43200,475200,6))
xticklabels({'Monday','Tuesday','Wendsday','Thursday','Friday', 'Saturday'}) % Label xtick at every 12 hours
xlim([-10000 518400])
ylim([minA,maxA+50])
ylabel('Active power (W)')
title('Weekly Patterns')
x=linspace(86400,475200,5); % Draw a vertical dashed line for each day
xline(x,'--k')
grid, grid minor
and show this figure which is not prefect for xticks and xline
##### 1 CommentShow NoneHide None
Rik on 29 Jun 2023
Your problems seem to be originating from the way you define the x-postitions in your plot.
How did you determine the cause is with xticklabels and xline?
Determining the days based on the hardcoded sampling rates seems very fragile to me. Why not rescale the values in t to range between 0 and 6? That way you rescale your data to the days, instead of rescaling the days to fit your sampling rate.

Steven Lord on 29 Jun 2023
If you have data that is time-based, why not plot the data using a datetime array then specify the ticks and tick labels using datetime values?
T = datetime('today');
d = T:hours(6):(T + days(4));
y = 1:length(d);
plot(d, y, 'o-')
xticks(d(1:4:end))
xline(d(3:4:end), '--k')
xline(T+(hours(12):hours(18):days(4)), 'c:', 'LineWidth', 4) % Make it wider so it is more visible

### Categories

Find more on Log 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!