hvplot icon indicating copy to clipboard operation
hvplot copied to clipboard

Coordinate label of a multi-element chart from an xarray Dataset lacks long name and units

Open stanwest opened this issue 3 years ago • 0 comments

Relevant software versions are collapsed here.
# Name                    Version                   Build  Channel
bokeh                     2.4.3            py39haa95532_0
holoviews                 1.15.0           py39haa95532_0
hvplot                    0.8.0            py39haa95532_0
python                    3.9.12               h6244533_0
xarray                    2022.6.0           pyhd8ed1ab_1    conda-forge

Given an xarray.Dataset that contains multiple data variables and a coordinate with "long_name" and/or "units" attributes, a multi-element plot created from that dataset labels the coordinate with only the coordinate's name instead of its long name and units:

In [1]: import holoviews, hvplot.xarray, xarray

In [2]: dset = xarray.Dataset(
   ...:     {"u": ("t", [1, 3]), "v": ("t", [4, 2])},
   ...:     coords={"t": ("t", [0, 1], {"long_name": "time", "units": "s"})},
   ...: )

In [3]: ndoverlay = dset.hvplot.line()

In [4]: ndoverlay.ddims[0]
Out[4]: Dimension('t')

In [5]: holoviews.render(ndoverlay).xaxis.axis_label
Out[5]: 't'

The same is true for an NdLayout returned by dset.hvplot.line(subplots=True).

In contrast, a single-element plot created from one data variable has coordinates labeled with the long name and units:

In [6]: curve = dset["u"].hvplot.line()

In [7]: curve.kdims[0]
Out[7]: Dimension('t', label='time', unit='s')

In [8]: holoviews.render(curve).xaxis.axis_label
Out[8]: 'time (s)'

Similarly, a single-element bar plot dset.hvplot.bar() of both variables in the dataset honors the long name and units:

In [9]: dset.hvplot.bar().kdims[0]
Out[9]: Dimension('t', label='time', unit='s')

A possible cause is that, whereas the HoloViewsConverter.single_chart and other plot methods apply the long name and units stored in self._redim by calling the redim(**self._redim) method on the chart [code], the HoloViewsConverter.chart method lacks such a call for the multi-element case [code]. Could the call be included in the HoloViewsConverter.chart method?

To work around the difference, the user may re-dimension the returned overlay, although converting the attributes from the dataset is a bit verbose:

In [10]: labeled_ndoverlay = ndoverlay.redim(**{"t": {
    ...:     hname: dset["t"].attrs[xname]
    ...:     for hname, xname in [("label", "long_name"), ("unit", "units")]
    ...: }})

In [11]: labeled_ndoverlay.ddims[0]
Out[11]: Dimension('t', label='time', unit='s')

stanwest avatar Aug 03 '22 13:08 stanwest