1

I'm trying to set the tick frequency on an xarray line plot. I managed to set the limits but I can't get the ticklocator to work. This should work similar to pandas.

import cftime
import xarray

fig=plt.figure()

ax0=plt.subplot(111)

xr_2d_data.plot(ax=ax0)

xmin = cftime.num2date(0*365, 'days since 0000-01-01 00:00:00', calendar='noleap')
xmax = cftime.num2date(1000*365, 'days since 0000-01-01 00:00:00', calendar='noleap')
ax0.set_xlim(xmin, xmax)

The above works, but if I try to set a locator , it does not work e.g.

import matplotlib.dates as mdates
ax0.xaxis.set_major_locator(mdates.YearLocator(100, month=1, day=1))
ax0.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
  • xarray depends on `nc-time-axis` to plot cftime dates. Within `nc-time-axis` they have written [their own cftime-compatible locator object](https://github.com/SciTools/nc-time-axis/blob/531dd0da83f2ea6b351f129559ccd4e8b0ccbb5f/nc_time_axis/__init__.py#L88); unfortunately the matplotlib locator objects (e.g. `YearLocator`) are not cftime-compatible. It could be worth posting an issue in the [`nc-time-axis`](https://github.com/SciTools/nc-time-axis) repo suggesting the addition of cftime-compatible matplotlib-like locators. – spencerkclark Jan 04 '20 at 12:54

0 Answers0