필터 지우기
필터 지우기

Why are the ticks of the datetick function unevenly spaced

조회 수: 2 (최근 30일)
ForTex
ForTex 2017년 6월 12일
답변: Peter Perkins 2017년 6월 20일
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