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

datetime locator doesn't work with cftime #126

Open
bradyrx opened this issue Mar 3, 2020 · 5 comments
Open

datetime locator doesn't work with cftime #126

bradyrx opened this issue Mar 3, 2020 · 5 comments

Comments

@bradyrx
Copy link
Collaborator

bradyrx commented Mar 3, 2020

Code sample, a copy-pastable example if possible

import proplot as plot
import numpy as np
import xarray as xr

times = xr.cftime_range('1990', '2000', freq='M')
data = xr.DataArray(np.random.rand(len(times)),
    dims=['time'],
    coords=[times])

f, ax = plot.subplots(aspect=2)

ax.plot(data.time, data)

# ax.format(xlocator='year')
# ax.format(xformatter='concise')

Actual result vs. expected result

The x datetime axis appears by default (with the comments included above), but when using ax.format(xlocator='year') or any locator, the xticks and labeling completely disappear.

When using any formatter (e.g. xformatter='concise') it breaks with the following error:

ValueError: Cannot convert -3652 to a date.  This often happens if non-datetime values are passed to an axis that expects datetime objects.
@lukelbd lukelbd added bug feature and removed bug labels Mar 3, 2020
@lukelbd
Copy link
Collaborator

lukelbd commented Mar 3, 2020

Unfortunately this bug is related to the incompatibility of "cftime" axes with native matplotlib datetime locators, not a proplot bug:

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
import xarray as xr

times = xr.cftime_range('1990', '2000', freq='M')
data = xr.DataArray(
    np.random.rand(len(times)),
    dims=['time'],
    coords=[times]
)

f, ax = plt.subplots()

ax.plot(data.time, data)
ax.xaxis.set_major_locator(mdates.YearLocator(1))

bug

It appears the default locator for cftime axes is a special nc_time_axis locator:

In [5]: ax.xaxis.get_major_locator()
Out[5]: <nc_time_axis.NetCDFTimeDateLocator at 0x1222c2898>

And unforunately, the nc_time_axis module does not seem to have any other available tick locators. You should request this feature on the nc-time-axis github page.

@lukelbd lukelbd closed this as completed Mar 3, 2020
@lukelbd lukelbd added support and removed feature labels Mar 3, 2020
@bradyrx
Copy link
Collaborator Author

bradyrx commented Mar 3, 2020

Thanks @lukelbd, I'll move this over there when I get a chance.

@bradyrx
Copy link
Collaborator Author

bradyrx commented Mar 23, 2020

I'm reopening this based on our discussion at SciTools/nc-time-axis#41. It looks like nc-time-axis is sort of a dead repo. This is on the longer-term list for proplot developments.

@bradyrx bradyrx reopened this Mar 23, 2020
@lukelbd lukelbd added feature and removed support labels Mar 23, 2020
@lukelbd
Copy link
Collaborator

lukelbd commented Mar 23, 2020

Works for me

@lukelbd
Copy link
Collaborator

lukelbd commented May 19, 2020

After the reorganization, the cftime additions should go in ticker.py. This isn't a priority for me right now, but this is an ideal issue for collaboration if someone else wants to try

@lukelbd lukelbd closed this as completed May 19, 2020
@lukelbd lukelbd reopened this May 19, 2020
@lukelbd lukelbd added this to the Version 0.10 milestone Jul 21, 2021
@lukelbd lukelbd removed this from the Version 0.10 milestone Jan 25, 2022
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

2 participants