Borrar filtros
Borrar filtros

How to add minor ticks to a time series

123 visualizaciones (últimos 30 días)
Candice Cooper
Candice Cooper el 10 de Ag. de 2021
Comentada: Candice Cooper el 11 de Ag. de 2021
I have a time series spanning 24 years. The dates are in matlab datenum format, and I am using
datetick('x','yyyy')
to add the dates to the x axis. This puts the years as major ticks. I want to add minor ticks for each month within each year, but am struggling to figure out how to do so. Would it be possible to just specify the number of minor ticks between each major tick? Any help is appreciated! Thanks!

Respuesta aceptada

Dave B
Dave B el 10 de Ag. de 2021
Editada: Dave B el 10 de Ag. de 2021
You can control the minor tick spacing with the MinorTickValues property with the XAxis property on the Axes...
% some fake data
x = datenum(datetime(2001:2024,1,1));
y=rand(24,1);
plot(x,y)
datetick('x','yyyy')
set(gca,'XMinorTick','on')
xl = xlim;
xAx = get(gca,'XAxis');
xAx.MinorTickValues=linspace(xl(1),xl(2),288);
Obviously it's indistinguishable, but worth noting these aren't months they're 12ths of years...
Things are better with datetime than datenum, although the result looks the same it makes it a little easier to think/code about date units (and the ticks are now at actual month beginnings, as crazy as our calendar is!):
x=datetime(2001:2024,1,1);
plot(x,y)
xlim([datetime([2000 2024],1,1)])
xticks(datetime(2000:2:2024,1,1))
xtickformat('yyyy')
set(gca,'XMinorTick','on')
xAx=get(gca,'XAxis');
xAx.TickLabelRotation=30;
xl=xlim;
xAx.MinorTickValues = xl(1):calmonths(1):xl(2);

Más respuestas (0)

Categorías

Más información sobre Calendar en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by