Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Description
Bug summary
DateFormatter
produces wrong values for a time axis withcftime
values
Code for reproduction
# Needed modulesimportxarrayasxrimportcftime,matplotlib.pyplotaspltfrommatplotlib.datesimportDateFormatter,YearLocatorimportnc_time_axis# Open data on ESGF serverd_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 variabletas=d_tas['tas'][0:800,34,45].squeeze()# A few checksprint (tas.time.values[0] )print (tas.time.attrs )# Plotsfig,axs=plt.subplots (nrows=2,ncols=2,figsize=(10,10) )axs=axs.ravel()## Very simple plotax=axs[0]ax.plot (tas.time,tas )ax.set_title ('Correct plot\nCorrect time axis')## Add formatting : \!/ Time axis is shiftedax=axs[1]ax.plot (tas.time,tas )ax.xaxis.set_major_formatter (DateFormatter ('%Y-%M'))ax.set_title ('Bug using DateFormatter\nTime axis is shifted by about 30 years')## Solution using nc_time_axisax=axs[2]ax.plot (tas.time,tas )formatter=nc_time_axis.CFTimeFormatter ("%Y-%m","noleap")ax.xaxis.set_major_formatter (formatter )ax.set_title ('Solution using nc_time_axis')plt.savefig ('BugMplCftime.png')
Actual outcome
The first, simple, plot is correct.
The second plot usesDateFormatter
. The time labels are wrong by about 30 years. I'm very puzzled, and don't know it is amatplotlib
or acftime
issue ?
The third plot usenc-time-axis
to correct the time axis values. Solution suggested by@spencerkclark (in Unidata/cftime issue#303Unidata/cftime#303) and independantly by@jypeter.
Seanc-time-axis
athttps://nc-time-axis.readthedocs.io/en/latest/
Expected outcome
Matplotlib
should at least raise an error when meeting object that it can not handle properly
If it could produces correct labels, it will be nice !
Additional information
- Do you maybe even know a fix?
Usingnc_time_axis
. See above.
Operating system
OS/X
Matplotlib Version
3.6.1
Matplotlib Backend
Qt5Agg
Python version
3.10.8
Jupyter version
3.4.8
Installation
conda