Xarray: problem with time axis values in line plot

Created on 2 Sep 2020  路  2Comments  路  Source: pydata/xarray

When I run the following code inside a jupyter notebook, the values on the x axis (time) in the generated plot, inserted after the code, appear to run from 0 to ~4.
I expect them to run from 1 to 4, like the time values do.
I can't tell if this is a problem with what xarray passes to nc_time_axis, or if it's a problem with nc_time_axis itself.
Could this be looked into please?

import cftime
import xarray as xr

time_vals = [cftime.DatetimeNoLeap(1+year, 1+month, 15)
             for year in range(3) for month in range(12)]

x_vals = [time_val.year + time_val.dayofyr / 365.0
          for time_val in time_vals]

x_da = xr.DataArray(x_vals, coords=[time_vals], dims=["time"])

x_da.plot.line("-o");

image

Environment:

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.8 | packaged by conda-forge | (default, Jul 31 2020, 02:25:08)
[GCC 7.5.0]
python-bits: 64
OS: Linux
OS-release: 3.10.0-1127.13.1.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
libhdf5: 1.10.6
libnetcdf: 4.7.4

xarray: 0.16.0
pandas: 1.1.1
numpy: 1.19.1
scipy: 1.5.2
netCDF4: 1.5.4
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: 2.4.0
cftime: 1.2.1
nc_time_axis: 1.2.0
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: 1.3.2
dask: 2.14.0
distributed: 2.14.0
matplotlib: 3.3.1
cartopy: 0.18.0
seaborn: 0.10.1
numbagg: None
pint: 0.15
setuptools: 49.6.0.post20200814
pip: 20.2.2
conda: None
pytest: 6.0.1
IPython: 7.17.0
sphinx: None

cftime upstream issue

Most helpful comment

I looked into the nc_time_axis issue a bit more. It appears it is a label formatting problem. The ticks actually make sense, but are labeled in a confusing way. If I force the date format pattern to be "%Y-%m-%d" in nc_time_axis -- note there's no way to do this without modifying the source code -- I get a plot that looks like this:

updated

We can see the ticks are really 0000-12-01, 0001-10-01, 0002-08-01, and 0003-06-01. The main problem, I think, is that nc_time_axis is choosing a monthly tick resolution, but using a format that would only make sense if the ticks had an annual resolution. So it should be picking a label format of "%Y-%m" instead of "%Y". In general it looks like there should be more work in nc_time_axis to make the logic that chooses the tick resolution and label format consistent.

I opened an issue in nc_time_axis to track this: https://github.com/SciTools/nc-time-axis/issues/48.

All 2 comments

Thanks for raising this @klindsay28; very clean example. This looks like an nc_time_axis issue, since I can reproduce it outside of xarray:

import cftime
import matplotlib.pyplot as plt
import nc_time_axis

time_vals = [cftime.DatetimeNoLeap(1+year, 1+month, 15)
             for year in range(3) for month in range(12)]

x_vals = [time_val.year + time_val.dayofyr / 365.0
          for time_val in time_vals]

fig, ax = plt.subplots(1, 1)
ax.plot(time_vals, x_vals, "-o")

example

I'll do some further work to try and diagnose what the issue is there.

I looked into the nc_time_axis issue a bit more. It appears it is a label formatting problem. The ticks actually make sense, but are labeled in a confusing way. If I force the date format pattern to be "%Y-%m-%d" in nc_time_axis -- note there's no way to do this without modifying the source code -- I get a plot that looks like this:

updated

We can see the ticks are really 0000-12-01, 0001-10-01, 0002-08-01, and 0003-06-01. The main problem, I think, is that nc_time_axis is choosing a monthly tick resolution, but using a format that would only make sense if the ticks had an annual resolution. So it should be picking a label format of "%Y-%m" instead of "%Y". In general it looks like there should be more work in nc_time_axis to make the logic that chooses the tick resolution and label format consistent.

I opened an issue in nc_time_axis to track this: https://github.com/SciTools/nc-time-axis/issues/48.

Was this page helpful?
0 / 5 - 0 ratings