How to have a different interval between xticks and xticklabels on a datetime plot?
Show older comments
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
More Answers (1)
"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 Axis Labels 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!
