Skip to content

Matplotlib date formatter and cftime : incompatible ? #303

@oliviermarti

Description

@oliviermarti
# 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions