datetick does not set x-ticks accurately
Mostrar comentarios más antiguos
Dear all,
I am experiencing a problem with the datetick function. I am working on Matlab R2014a and the problem is the following.
I have some time series data, which I want to plot and I would like to have the x-ticks in yyyy-qq format. For this purpose I am using the functions datenum and datetick. However, dateticks generates the x-ticks sometimes inaccurately; it misses sometimes certain quarters and jumps directly to the next (e.g. the ticks are 2015-Q4 2016-Q1 2016-Q2 2016-Q3 2017-Q1), so 2016-Q4 is apparently missing. However, the space between the ticks is equal, so this must be an error.
This code reproduces the problem:
%%Problem with x-ticks using datetick
data_cpi=0.2+randn(229,1);
startDate = datenum(1998,12,1);
endDate = datenum(2017,12,1);
month = linspace(startDate,endDate,size(data_cpi,1))';
figure
plot(month,data_cpi)
xlim([month(end-36) month(end)])
datetick('x','yyyy-qq','keepticks','keeplimits')
Is this a bug and how could I fix this problem?
Thanks for the help in advance!
Best,
Diego
Respuesta aceptada
Más respuestas (1)
Kirby Fears
el 17 de Sept. de 2015
Editada: Kirby Fears
el 17 de Sept. de 2015
Hi Diego,
When you use the 'keepticks' option in your datetick function call
datetick('x','yyyy-qq','keepticks','keeplimits')
you are telling Matlab to convert the datenumbers previously shown on those ticks to 'yyyy-qq'. Matlab is rounding a specific date to a year and quarter representation, which results in 2016-Q4 getting skipped. Since the kept ticks are (for me) 11 ticks spaced evenly over 37 months, you're not getting 1 tick per quarter at all times. If you had 12 ticks over 36 months, it would work fine.
You can fix this by explicitly choosing the x values & number of xticks before applying datetick with the 'keepticks' option. Be sure to pick good dates that round evenly to yyyy-qq format. Use the link below as a guide for setting the x ticks.
Hope this helps.
Categorías
Más información sobre Dates and Time en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!