Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem with time axis values in line plot #4401

Closed
klindsay28 opened this issue Sep 2, 2020 · 4 comments
Closed

problem with time axis values in line plot #4401

klindsay28 opened this issue Sep 2, 2020 · 4 comments

Comments

@klindsay28
Copy link

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

@spencerkclark
Copy link
Member

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.

@spencerkclark
Copy link
Member

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: SciTools/nc-time-axis#48.

@spencerkclark
Copy link
Member

This will be fixed in the next release of nc-time-axis, so I will go ahead and close this issue. Thanks for your patience @klindsay28.

@spencerkclark
Copy link
Member

nc-time-axis version 1.4.0 has now been released and is available on both PyPI and conda-forge. It fixes this issue and a number of others -- see the newly added release notes and documentation for more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants