Skip to content

Commit

Permalink
Add support for nc_time_axis in matplotlib backend
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Dec 5, 2018
1 parent 5ec1e7d commit 88bebf3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
8 changes: 5 additions & 3 deletions holoviews/core/data/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@

try:
import cftime
util.datetime_types += (
cftime_types = (
cftime._cftime.DatetimeGregorian,
cftime._cftime.Datetime360Day,
cftime._cftime.DatetimeJulian,
cftime._cftime.DatetimeNoLeap,
cftime._cftime.DatetimeProlepticGregorian)
cftime._cftime.DatetimeProlepticGregorian
)
util.datetime_types += cftime_types
except:
pass
cftime_types = ()


class XArrayInterface(GridInterface):
Expand Down
34 changes: 34 additions & 0 deletions holoviews/plotting/mpl/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np
import matplotlib
from matplotlib import units as munits
from matplotlib import ticker
from matplotlib.colors import cnames
from matplotlib.lines import Line2D
Expand All @@ -14,6 +15,8 @@
from matplotlib.rcsetup import (
validate_capstyle, validate_fontsize, validate_fonttype, validate_hatch,
validate_joinstyle)

from ...core.data.xarray import cftime_types
from ...core.util import LooseVersion, _getargspec, basestring, is_number
from ...element import Raster, RGB, Polygons
from ..util import COLOR_ALIASES, RGB_HEX_REGEX
Expand Down Expand Up @@ -334,3 +337,34 @@ def polygons_to_path_patches(element):
subpath.append(PathPatch(Path(vertices, codes)))
mpl_paths.append(subpath)
return mpl_paths

try:
if cftime_types:
from nc_time_axis import NetCDFTimeConverter, CalendarDateTime
else:
import matplotlib.dates import DateConverter
NetCDFTimeConverter = DateConverter
nc_axis_available = True
except:
import matplotlib.dates import DateConverter
NetCDFTimeConverter = DateConverter
nc_axis_available = False

class CFTimeConverter(NetCDFTimeConverter):

@classmethod
def convert(cls, value, unit, axis):
if not nc_axis_available:
raise ValueError('In order to display cftime types with '
'matplotlib install the nc_time_axis '
'library using pip or from conda-forge '
'using:\n\tconda install -c conda-forge '
'nc_time_axis')
if isinstance(value, cftime_types):
value = CalendarDateTime(cftime.datetime(*value.timetuple()[0:6]), value.calendar)
elif isinstance(value, np.ndarray):
value = np.array([CalendarDateTime(cftime.datetime(*v.timetuple()[0:6]), v.calendar) for v in value])
return super(CFTimeConverter, cls).convert(value, unit, axis)

for cft in cftime_types:
munits.registry[cft] = CFTimeConverter()

0 comments on commit 88bebf3

Please sign in to comment.