Plotting timetable data: how to add x-axis ticks, labels and gridlines on 1st and 15th day of each month

361 vues (au cours des 30 derniers jours)
I have a timetable with mean daily air temperature data recorded for nine months (1st June 2018 to 1st March 2019).
Ta_plot2.jpg
I would like to plot it with:
  • x-axis ticks, labels and major gridlines on 1st day of each month
  • x-axis ticks and minor gridlines on 15th day of each month.
Currently I have the following code, which adds 10 equally spaced ticks, labels and gridlines (more or less every 30 days):
% Create a plot, set axis limits and y label
plot(indata.time,indata{:,1});
xlim(datetime([2018 2019],[6 3],[1 1]))
ylabel('Mean Ta (*C)');
% Add equally spaced ticks (one every ~30 days)
xData = linspace(indata1.time(1),indata1.time(end),10);
d.XTick = xData;
% Add labels
tx = get(gca,'XTickLabel'); set(gca,'XTickLabel',tx,'fontsize',fsize,'FontWeight','bold');
ty = get(gca,'XTickLabel'); set(gca,'XTickLabel',ty,'fontsize',fsize,'FontWeight','bold');
datetick('x','dd mmm yy','keeplimits','keepticks');
% Add gridlines
hAx=gca;
set(hAx,'YGrid','on','XGrid','on')
set(hAx,'xminorgrid','on','yminorgrid','on')
which results in this figure:
Ta_plot.jpg
Is there a simple way to:
1) set ticks, labels and major gridlines on 1st day of each month?
2) add minor gridlines? (my code works fine with minor horizontal gridlines, but not vertical ones)
Thanks in advance!

Réponse acceptée

dpb
dpb le 27 Fév 2019
t=[datetime(2018,6,1):datetime(2019,3,1)].'; % build a sample dataset...
tt=timetable(t,randn(size(t))+10); % just some data to go with the dates
plot(tt.t,tt.Var1) % and a plot
hAx=gca; % get axes handle
ixmajor=find(day(t)==1); % indices for first of month in time vector
hAx.XTick=tt.t(ixmajor); % set ticks appropriately
hAx.XAxis.TickLabelFormat='ddMMMyy'; % set the format to concise form
hAx.XAxis.TickLabelRotation=20; % and make some room to display
ixminor=find(day(tt.t)==15); % ides of each month...
hAx.XAxis.MinorTickValues=tt.t(ixminor); % set minor tick locations
hAx.XAxis.MinorTick='on'; % and display 'em
hAx.YMinorGrid='on'; % and the grids, too...
hAx.XMinorGrid='on';
results in
untitled.jpg
The key item is to get away from old deprecated datenum and use the new datetime and the time axis ruler object instead of datetick which was always difficult at best. This is simple enough could have managed, but there's no need.
  5 commentaires
dpb
dpb le 28 Fév 2019
Modifié(e) : dpb le 28 Fév 2019
For what definition of "work"? Presuming correct the typo to use the actual axis handle variable name of hAx instead of the mistyped hAs in the comment, the grid line will be drawn.
It is pretty fine line and may not be easily distinguishable from the others...just
plot(tt.t,tt.Var1)
hAx=gca;
hAx.XGrid='on';
will illustrate the grid lines are on.
If you don't think that's dark enough, try
hAx.GridAlpha=0.25; % default is 0.15
to darken up the grid lines. Fortunately, TMW did leave the granularity that there are both GridAlpha and MinorGridAlpha properties.
You just have to dig deeply into all the available axis and ruler properties to find the ones needed to build the desired effect.
Usually one can manage; occasionally specialized plots run into real roadblocks but here you should be able to accomplish the desired result. Admittedly, it's not so obvious that 'GridAlpha' would be a property of interest for the newish users of handle graphics when are trying to "bolden-up" a line...that comes mostly from "time in grade", unfortunately.

Connectez-vous pour commenter.

Plus de réponses (1)

Roger Parkyn
Roger Parkyn le 20 Mai 2021
Another way to get a vector of monthly datetimes to use for the ticks would be:
t_monthly=[datetime(2018,6,1):calmonths(1):datetime(2019,3,1)].'
then can use the abovementioned:
hAx.XAxis.MinorTickValues=t_monthly; % set minor tick locations
The mid-month times could be:
t_mid_monthly = t_monthly(1:end-1) + days(15);

Catégories

En savoir plus sur Calendar dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by