How to have a different interval between xticks and xticklabels on a datetime plot?

42 views (last 30 days)
I have measurements at 2-minute frequency for a period of 41 days and want to plot a variable over that period with an xtick mark appearing at each midnight, but an xticklabel (i.e. date info) appearing every other midnight i.e. the data start on July 31st, i'd like the next label to be 2nd Aug (missing 1st Aug) etc
I've looked at the DatetimeRuler property help, but the code below produces an xtick mark plus label for each midnight (one ticklabel is not 'skipped').
Following other help entries, my approach has been to create a string variable for the xticklabel that has blank values for every other midnight label, but this doesn't produce the hoped-for plot when DatetimeRuler is being used. Thanks for any help.
start_dt=datetime('2018-07-31 09:42:00');
end_dt=datetime('2018-09-09 09:40:00');
x_datetimes=(start_dt:minutes(2):end_dt); %Create a 2-minute interval for the time period
start_date=dateshift(start_dt,'start','day'); %Find start date
end_date=dateshift(end_dt,'start','day');
x_period_dates=(start_date:days(1):end_date); %Build list of n=41 days' dates
midnight=duration('00:00:00','InputFormat','hh:mm:ss'); %Create a midnight duration
x_dates_midnights=datetime((x_period_dates+midnight),'Format','yyyy-MM-dd HH:mm:ss'); %Make n=41 date+midnight datetimes
x_dates_midnights_Wgaps_str=string(x_dates_midnights); %Make a string variable for dates with midnights
x_dates_midnights_Wgaps_str(2:2:end)=''; %Replace every other date/midnight string with empty
figure;
plot(x_datetimes,rand(1,length(x_datetimes))); %Plot using a datetime on x-axis
xmin = min(x_dates_midnights);
xmax = max(x_dates_midnights);
xtix = xmin:days(1):xmax;
ax=gca;
ax.XAxis.TickValues=xtix-1; % -1 to not show final tick
ax.XAxis.TickLabel=x_dates_midnights_Wgaps_str;
xtickformat('MMM-dd HH:mm')

Accepted Answer

Adam Danz
Adam Danz on 12 Apr 2024 at 16:06
Edited: Adam Danz on 15 Apr 2024 at 14:20
The x_period_dates variable is all you need. It's a datetime vector containing all of the dates at midnight. You just need to set the axes xtick values to every odd index of x_period_dates to label every second day.
figure;
plot(x_datetimes,rand(1,length(x_datetimes))); %Plot using a datetime on x-axis
ax = gca;
ax.XTick = x_period_dates(1:2:end)
To show a tick for every day at midnight and label every second day, one idea is to follow the code block above by setting the minor tick values.
ax.XAxis.MinorTickValues = x_period_dates;
Alternatively, you can set the xticks and xticklabels independently.
figure;
plot(x_datetimes,rand(1,length(x_datetimes))); %Plot using a datetime on x-axis
ax = gca;
ax.XTick = x_period_dates;
xdateLabels = string(x_period_dates);
xdateLabels(2:2:end) = "";
ax.XTickLabel = xdateLabels;
  4 Comments
Adam Danz
Adam Danz on 15 Apr 2024 at 14:45
Thanks for the reminder! I've updated my answer with two approaches to show a daily tick and label every second date.

Sign in to comment.

More Answers (1)

Voss
Voss on 15 Apr 2024 at 14:33
"xtick mark appearing at each midnight, but an xticklabel (i.e. date info) appearing every other midnight"
Using xtickformat sets all the tick labels, overwriting your previously set tick labels (where every other one was empty). Avoid using xtickformat, and instead set the Format of x_dates_midnights to 'MMM-dd HH:mm'.
start_dt=datetime('2018-07-31 09:42:00');
end_dt=datetime('2018-09-09 09:40:00');
x_datetimes=(start_dt:minutes(2):end_dt); %Create a 2-minute interval for the time period
start_date=dateshift(start_dt,'start','day'); %Find start date
end_date=dateshift(end_dt,'start','day');
x_period_dates=(start_date:days(1):end_date); %Build list of n=41 days' dates
midnight=duration('00:00:00','InputFormat','hh:mm:ss'); %Create a midnight duration
x_dates_midnights=datetime((x_period_dates+midnight),'Format','MMM-dd HH:mm'); %Make n=41 date+midnight datetimes
x_dates_midnights_Wgaps_str=string(x_dates_midnights); %Make a string variable for dates with midnights
x_dates_midnights_Wgaps_str(2:2:end)=''; %Replace every other date/midnight string with empty
figure;
plot(x_datetimes,rand(1,length(x_datetimes))); %Plot using a datetime on x-axis
xmin = min(x_dates_midnights);
xmax = max(x_dates_midnights);
xtix = xmin:days(1):xmax;
ax=gca;
ax.XAxis.TickValues=xtix-1; % -1 to not show final tick
ax.XAxis.TickLabel=x_dates_midnights_Wgaps_str;

Categories

Find more on Visual Exploration in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!