Skip to content

Support using xarray non-dimensioned coordinates as explicit kdims. #6131

@alcrene

Description

@alcrene

This is somewhat a complement issue to the treatment of xarray non-dimensioned coordinates #5214 .
That issue concerned the behaviour non-dimensioned with auto-populated widgets. With good reason non-dimensioned coordinates are not treated as kdims in that case.

However I think it would still be very useful if they could be used with kdims when explicitely specified by a user. Consider the following example from the xarray docs:

In [65]: ds.coords
Out[65]: 
Coordinates:
    lat             (x, y) float64 42.25 42.21 42.63 42.59
    lon             (x, y) float64 -99.83 -99.32 -99.79 -99.23
  * time            (time) datetime64[ns] 2014-09-06 2014-09-07 2014-09-08
    reference_time  datetime64[ns] 2014-09-05
    day             (time) int64 6 7 8

Here 'lat' and 'lon' are non-dimensioned coordinates, yet I would think that it makes sense to want to use them as kdims to a scatter plot or heatmap.

As another example, consider a simulation that has multiple variants of its time axis:

  • simulator step (int)
  • simulation time (float)
  • real-world time (float)

Only one of this can be a dimensioned coordinate in the DataArray, yet all of them could sensibly be used as a plotting axis.

Finally, it may be worth noting that plotting is one of the explicit intentions for non-dimensioned coordinates according to the docs I linked above.

Describe the solution you'd like

Given an xarray DataArray,

da = xr.DataArray(np.arange(6).reshape(6,1), dims=["x", "stat"], coords={"x": np.arange(6), "stat": ["mean"], "x2": ('x', np.arange(6)/6)})

it is very easy to plot using dimensioned coordinates as kdims:

hv.Curve(da, kdims="x", vdims="stat")

I would like the same to be true for non-dimensioned coordinates

hv.Curve(da, kdims="x2", vdims="stat")

Workaround

One approach that already works today is to first convert to a dataframe

hv.Curve(da.to_dataframe(name="mean"), kdims="x2", vdims="mean")

This isn’t too bad (even though the vdim name needs to be repeated), but it still feels like xarray’s annotation abilities (here to specify alternative axes) are being wasted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions