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

Matplotlib date formatter and cftime : incompatible ? #303

Open
oliviermarti opened this issue Jan 11, 2023 · 8 comments
Open

Matplotlib date formatter and cftime : incompatible ? #303

oliviermarti opened this issue Jan 11, 2023 · 8 comments

Comments

@oliviermarti
Copy link

# Needed modules
import xarray as xr
import cftime, matplotlib.pyplot as plt

# Open data on ESGF server
d_tas = xr.open_dataset ("https://vesg.ipsl.upmc.fr/thredds/dodsC/cmip5/output1/IPSL/IPSL-CM5A-LR/rcp85/day/atmos/day/r1i1p1/v20111103/tas/tas_day_IPSL-CM5A-LR_rcp85_r1i1p1_20060101-22051231.nc", decode_times=True, use_cftime=True)

# Define variable
tas = d_tas['tas'][0:3650,34,45].squeeze()
print ( tas.time.values[0] )
print ( tas.time.attrs )

# Plots
from matplotlib.dates import DateFormatter, YearLocator

fig, axs = plt.subplots (nrows=2, ncols=2, figsize=(10,10) )
axs=axs.ravel()

# Very simple plot
ax = axs[0]
ax.plot ( tas.time, tas )

# Add formatting : \!/ Time axis is shifted
ax = axs[1]
ax.plot ( tas.time, tas )
ax.xaxis.set_major_formatter ( DateFormatter ('%Y'))

In the second plot, the time labels are wrong by about 30 years. I'm very puzzled, and don't know it is a matplotlib or a cftime issue ?

Thanks for your help.

Olivier
BugMplCftime

@spencerkclark
Copy link
Collaborator

Yes, we should maybe find some way in which this raises an error instead of silently producing incorrect results, but special formatters are indeed needed for cftime dates. Please take a look at nc-time-axis and specifically this example for some inspiration.

@oliviermarti
Copy link
Author

Thanks for the link, it works perfectly :-)

And yes, it should raise an error. But who should be concerned : matplotlib, cftime, ... ?

@spencerkclark
Copy link
Collaborator

Awesome! I think it would be best if matplotlib could handle this, but it would be ideal for their side if they could somehow do it without a dependence on cftime (e.g. just catching an unrecognized date type). I'd have to dig into the matplotlib code to see exactly how one might go about that. It could be worth opening an issue there.

@spencerkclark
Copy link
Collaborator

Thanks for posting this issue by the way.

@oliviermarti
Copy link
Author

Do you want me to post an issue on Matplotlib ? I would be the same than here, plus the fix :-)

@spencerkclark
Copy link
Collaborator

That would be great; you're welcome to tag me in the issue as well. I can try and serve as an intermediary for any questions about cftime or nc-time-axis.

@oliviermarti
Copy link
Author

OK. Will be done tomorrow (I'm in Europe, and a bit sleepy ... )

@spencerkclark
Copy link
Collaborator

Indeed no rush!

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

No branches or pull requests

2 participants