Why are the ticks of the datetick function unevenly spaced

2 次查看(过去 30 天)
Hey.
I have data from the start of 1953 to the end of 2016. The time vector is in datenum format and the resolution is one hour. There is one year of missing data with NaNs in 1952. When using the datetick-function, the ticks are unevenly spaced, every 2 or 3 years. What is causing this? What would you suggest as a fix?
Edit: apparently removing NaN-data from the start of the time series fixes the problem. Its very common for time series to have full years of data but having missing data at the start or the end, so this behaviour is probably unintended? Gonna leave this here for anyone with the same problem.
  1 个评论
dpb
dpb 2017-6-12
To "fix" it, you can simply compute tick spacing/locations as desired and set the 'xtick' property. datetick tries to do the best it can to set a reasonable number and resolution but isn't perfect.
If you've got missing data, what do you want on the axes, the first of the full year with data, the first year of the series and an empty stretch before the first actual data or the limits set at the beginning/end of actual data but the tick marks at the full years inbetween? Any of those is computable; just have to decide what one want to use.
If have recent release, I'd recommend the datetime class over venerable datenum; its internal plot logic may be more satisfactory but the same fixups are available if the class-specific plot function uses same internal logic.

请先登录,再进行评论。

回答(2 个)

the cyclist
the cyclist 2017-6-13
You can also use
datetick('x','keepticks')
to keep the original (presumably evenly spaced) tick locations. You can choose the specific tick locations prior to applying datetick, if necessary.

Peter Perkins
Peter Perkins 2017-6-20
You say "unevenly spaced", by which I think you mean, "the ticks are labelled at sometimes two and sometimes three year intervals." You have not supplied the code you used to generate that figure, so it's really impossible to diagnose. My best guess is that the format is only showing the year, but datetick has chosen ticks that are not exactly one year apart. I could easily be wrong. You could verify that by specifying something like 'mmm-yyyy' as the tick format.
But the cyclist's advice is good: datetime will do a much better job of this plot.

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by