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

how to best set lim? // allow string in convert? #178

Open
mathause opened this issue Nov 21, 2023 · 3 comments
Open

how to best set lim? // allow string in convert? #178

mathause opened this issue Nov 21, 2023 · 3 comments
Labels
New: Feature Highlight a new community raised "feature request" issue

Comments

@mathause
Copy link

✨ Feature Request

Should/ could allow passing a string to convert (of the NetCDFTimeConverter) be beneficial?

Motivation

I want to set the xlim of a nc-time axis. Is there a more convinent way than to pass cftime._cftime.datetime?

It seems I need to do:

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

data = np.random.randn(100)
time = xr.date_range("2000", "2100", freq="A", calendar="noleap")
da = xr.DataArray(data, coords={"time": time})

f, ax = plt.subplots()

da.plot(ax=ax)
ax.set_xlim(None, da.time.sel(time="2050").item())

It would be nice if we could pass a string - similar as we do with sel in xarray, e.g.:

ax.set_xlim(None, "2050")

Additional context

Click to expand this section...
Please add additional verbose information in this section e.g., references, screenshots, listings etc
@mathause mathause added the New: Feature Highlight a new community raised "feature request" issue label Nov 21, 2023
@ESadek-MO
Copy link

@spencerkclark Do you have any thoughts on this?

@SciTools/peloton wonders if this is doable at all.

@spencerkclark
Copy link
Member

In principle I think this would be doable, though it would take a little thought to get right. @mathause are you thinking that we would interpret the passed strings independent of whether they corresponded to the start or end of the interval, e.g. "2050" as an end interval would correspond to "2050-01-01T00:00:00", rather than "2050-12-31T23:59:59.999999" as it does in sel?

While matplotlib does not support passing strings for limits of datetime axes on its own (matplotlib/matplotlib#25219), it appears at least pandas has a converter that does. In pandas it seems the answer would be "2050-01-01T00:00:00", which I think makes sense, and is simpler to implement:

fig, ax = plt.subplots(1, 1)

times = pd.date_range("2049", periods=2500)
series = pd.Series(range(2500), index=times)
series.plot(ax=ax)
ax.set_xlim(None, "2050")

image

I'm not sure I have the bandwidth right now to implement this, but I would be open to reviewing a PR.

@trexfeathers
Copy link
Contributor

From @SciTools/peloton: is there a way of doing this without introducing further dependencies? Right now nc-time-axis has very few:

# core dependencies
- cftime >=1.5
- matplotlib-base
- numpy >=1.21

... which makes it nice and flexible with Xarray, Iris and others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
New: Feature Highlight a new community raised "feature request" issue
Projects
Status: No status
Development

No branches or pull requests

4 participants