pydata / pydata/xarray

Cannot plot multiindexed (stacked) coordinate as hue variable

Open
#4,562 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-plotting
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

import numpy as np
import pandas as pd
import xarray as xr

data = np.random.rand(50,5)

x_idx = np.linspace(0, 50)

mi_idx1 = ['a','b','c','d','e']
mi_idx2 = [1,2,3,4,5]

mi = pd.MultiIndex.from_arrays([mi_idx1,mi_idx2], names=['mi_idx1', 'mi_idx2'])

coords = {
    'x': x_idx,
    'mi': mi
}

da = xr.DataArray(data, coords=coords, dims = ['x', 'mi'])

da.plot(hue='mi')

It appears since version 0.16.0, that plotting with a multindex coordinate as a hue dimension no longer works. In version 0.15.1 I get a plot like this:

xarray_issue

However, when upgrading to 0.16.0 or 0.16.1 I get the following traceback:


ValueError                                Traceback (most recent call last)
<ipython-input-1-2de8705a6ec5> in <module>
     19 da = xr.DataArray(data, coords=coords, dims = ['x', 'mi'])
     20 
---> 21 da.plot(hue='mi')

~\anaconda3\envs\datanalysis\lib\site-packages\xarray\plot\plot.py in __call__(self, **kwargs)
    444 
    445     def __call__(self, **kwargs):
--> 446         return plot(self._da, **kwargs)
    447 
    448     # we can't use functools.wraps here since that also modifies the name / qualname

~\anaconda3\envs\datanalysis\lib\site-packages\xarray\plot\plot.py in plot(darray, row, col, col_wrap, ax, hue, rtol, subplot_kws, **kwargs)
    198     kwargs["ax"] = ax
    199 
--> 200     return plotfunc(darray, **kwargs)
    201 
    202 

~\anaconda3\envs\datanalysis\lib\site-packages\xarray\plot\plot.py in line(darray, row, col, figsize, aspect, size, ax, hue, x, y, xincrease, yincrease, xscale, yscale, xticks, yticks, xlim, ylim, add_legend, _labels, *args, **kwargs)
    293 
    294     ax = get_axis(figsize, size, aspect, ax)
--> 295     xplt, yplt, hueplt, xlabel, ylabel, hue_label = _infer_line_data(darray, x, y, hue)
    296 
    297     # Remove pd.Intervals if contained in xplt.values and/or yplt.values.

~\anaconda3\envs\datanalysis\lib\site-packages\xarray\plot\plot.py in _infer_line_data(darray, x, y, hue)
     66 
     67         if y is None:
---> 68             xname, huename = _infer_xy_labels(darray=darray, x=x, y=hue)
     69             xplt = darray[xname]
     70             if xplt.ndim > 1:

~\anaconda3\envs\datanalysis\lib\site-packages\xarray\plot\utils.py in _infer_xy_labels(darray, x, y, imshow, rgb)
    378         y, x = darray.dims
    379     elif x is None:
--> 380         _assert_valid_xy(darray, y, "y")
    381         x = darray.dims[0] if y == darray.dims[1] else darray.dims[1]
    382     elif y is None:

~\anaconda3\envs\datanalysis\lib\site-packages\xarray\plot\utils.py in _assert_valid_xy(darray, xy, name)
    410     if xy not in valid_xy:
    411         valid_xy_str = "', '".join(sorted(valid_xy))
--> 412         raise ValueError(f"{name} must be one of None, '{valid_xy_str}'")
    413 
    414 

ValueError: y must be one of None, 'mi_idx1', 'mi_idx2', 'x'

Setting x='x' does not fix the problem

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.8.3 (default, Jul 2 2020, 17:30:36) [MSC v.1916 64 bit (AMD64)]
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 142 Stepping 12, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: English_United States.1252
libhdf5: 1.10.4
libnetcdf: 4.7.3

xarray: 0.16.0
pandas: 1.0.5
numpy: 1.18.5
scipy: 1.5.0
netCDF4: 1.5.3
pydap: None
h5netcdf: None
h5py: 2.10.0
Nio: None
zarr: None
cftime: 1.2.1
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: 1.3.2
dask: 2.20.0
distributed: 2.20.0
matplotlib: 3.2.2
cartopy: None
seaborn: 0.10.1
numbagg: None
pint: 0.16.1
setuptools: 49.2.0.post20200714
pip: 20.1.1
conda: None
pytest: 5.4.3
IPython: 7.16.1
sphinx: 3.1.2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example with the shown xarray, pandas, NumPy, and Matplotlib setup, then inspect xarray/plot/plot.py and xarray/plot/utils.py around _infer_line_data and _infer_xy_labels. Done means da.plot(hue='mi') no longer raises for the MultiIndex coordinate and restores the behavior shown for version 0.15.1.

Written by the indexing model from the issue text.

Assessment

Tech stack
matplotlib, numpy, pandas, python
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.