Filter löschen
Filter löschen

Why are the ticks of the datetick function unevenly spaced

2 Ansichten (letzte 30 Tage)
ForTex
ForTex am 12 Jun. 2017
Beantwortet: Peter Perkins am 20 Jun. 2017
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 Kommentar
dpb
dpb am 12 Jun. 2017
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.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

the cyclist
the cyclist am 13 Jun. 2017
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 am 20 Jun. 2017
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.

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by