Stream: python-questions
Topic: time decoding problem with xarray and matplotlib contourf
Frank Bryan (Apr 16 2020 at 22:26):
I am running into a problem trying to plot a Hovmoller diagram of data form a Tropical Pacific mooring using contourf. Looking for suggestions for a work around. A simplifed version of the code follows:
ds_tao = xr.open_dataset('/glade/work/bryan/Observations/TAO/adcp0n140w_dy.cdf') u = ds_tao.u_1205.squeeze().transpose() u = u.where(u<1.0e35) # missing value=1.0e35 # This works fine and correctly decodes the dates u.plot(robust=True). # This creates a plot but does not decode the dates ulev = np.arange(-100.,100.,5.0) fig,ax = plt.subplots() cf = ax.contourf(u,levels=np.arange(-100.,100.,5.0)) # This fails fig,ax = plt.subplots() cf = ax.contour(ds_tao.time,ds_tao.depth,u,levels=ulev)
The final part of the traceback is
/glade/work/bryan/miniconda3/envs/analysis/lib/python3.7/site-packages/matplotlib/dates.py in _from_ordinalf(x, tz) 286 'non-datetime values are passed to an axis that ' 287 'expects datetime objects.'.format(ix)) --> 288 dt = datetime.datetime.fromordinal(ix).replace(tzinfo=UTC) 289 290 # Since the input date *x* float is unable to preserve microsecond OverflowError: signed integer is greater than maximum
Gustavo M Marques (Apr 16 2020 at 23:46):
Try:
cf = ax.contour(ds_tao.time.values,ds_tao.depth,u,levels=ulev)
Frank Bryan (Apr 17 2020 at 00:06):
Worked! Thanks Gustavo.
Anna-Lena Deppenmeier (Apr 17 2020 at 00:10):
Great!! That is way easier than my workaround.
Deepak Cherian (Apr 17 2020 at 16:27):
Alternatively ds_tao.u.plot.contourf(x="time", y="depth", levels=ulev)
?
Frank Bryan (Apr 17 2020 at 19:09):
Also works. Thanks Deepak.
Last updated: Jan 30 2022 at 12:01 UTC