Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use level of a MultiIndex for plotting? #3927

Closed
mathause opened this issue Apr 2, 2020 · 1 comment · Fixed by #3938
Closed

use level of a MultiIndex for plotting? #3927

mathause opened this issue Apr 2, 2020 · 1 comment · Fixed by #3938

Comments

@mathause
Copy link
Collaborator

mathause commented Apr 2, 2020

It would be nice to be able to use a level of a MultiIndex for plotting.

MCVE Code Sample

import numpy as np
import xarray as xr

da = xr.DataArray(
                  np.random.randn(10),
                  dims="x",
                  coords=dict(
                              a=("x", np.arange(10, 20)), 
                              b=("x", np.arange(1, 11))
                              )
                  )

da = da.set_index(x=["a", "b"])
da

This creates the following DataArray

<xarray.DataArray (x: 10)>
array([-1.34516338,  0.97644817, -0.24032189, -0.70112418, -0.8686898 ,
       -0.55607078,  0.56618151,  1.62847463,  0.84947296, -0.5775504 ])
Coordinates:
  * x        (x) MultiIndex
  - a        (x) int64 10 11 12 13 14 15 16 17 18 19
  - b        (x) int64 1 2 3 4 5 6 7 8 9 10

Is there a way to plot a line using one of the levels of the MultiIindex?

da.plot(x="a")

returns

ValueError: x must be either None or one of ('x')
da.plot()

returns

TypeError: Plotting requires coordinates to be numeric or
dates of type np.datetime64, datetime.datetime, cftime.datetime
or pd.Interval.

(which makes sense). If da is a 2D Variable the error is

ValueError: x and y must be coordinate variables

Expected Output

A line plot

Versions

Output of `xr.show_versions()`

INSTALLED VERSIONS

commit: b3bafee
python: 3.7.3 | packaged by conda-forge | (default, Jul 1 2019, 21:52:21)
[GCC 7.3.0]
python-bits: 64
OS: Linux
OS-release: 4.15.0-91-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
libhdf5: 1.10.5
libnetcdf: 4.7.1

xarray: 0.11.1+335.gb0c336f6
pandas: 0.25.3
numpy: 1.17.3
scipy: 1.3.1
netCDF4: 1.5.3
pydap: installed
h5netcdf: 0.7.4
h5py: 2.10.0
Nio: None
zarr: 2.3.2
cftime: 1.0.4.2
nc_time_axis: None
PseudoNetCDF: installed
rasterio: 1.1.0
cfgrib: 0.9.5.4
iris: None
bottleneck: 1.2.1
dask: 2.6.0
distributed: 2.6.0
matplotlib: 3.1.2
cartopy: 0.17.0
seaborn: 0.9.0
numbagg: None
pint: None
setuptools: 41.6.0.post20191101
pip: 19.3.1
conda: None
pytest: 5.2.2
IPython: 7.9.0
sphinx: None

@mathause
Copy link
Collaborator Author

mathause commented Apr 2, 2020

For the line plot it is here:

if x is not None and x not in darray.dims and x not in darray.coords:
raise ValueError("x " + error_msg)

For 2D plot it seems to be here

xarray/xarray/plot/utils.py

Lines 370 to 371 in b3bafee

elif any(k not in darray.coords and k not in darray.dims for k in (x, y)):
raise ValueError("x and y must be coordinate variables")

Both might need something like this:

any([x in da[dim].variable.level_names for dim in da.dims])

or maybe

any([x in da[coord].variable.level_names for coord in da.coords])

?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant