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

How to plot boolean vector with current xarray #3722

Closed
MeraX opened this issue Jan 24, 2020 · 3 comments · Fixed by #3766
Closed

How to plot boolean vector with current xarray #3722

MeraX opened this issue Jan 24, 2020 · 3 comments · Fixed by #3766

Comments

@MeraX
Copy link
Contributor

MeraX commented Jan 24, 2020

Dear all,

I'm trying to plot a vector with bool data. It used to work with xarray version 0.10.7 in an old setup, but it isn't working anymore. Am I doing something wrong or is there something new to consider?

I'm not hundred percent sure, if this problem is due to changes in xarray, numpy or something else

Code example

import xarray
xarray.DataArray([True, False]).plot()

Expected Output

A plot with a line from 1 to 0.

Problem Description

However, I get this error:

TypeError                                 Traceback (most recent call last)
<ipython-input-22-d911786d1072> in <module>
----> 1 xarray.DataArray([True, False]).plot()

~/python_venvs/python3/py3_18.04/lib/python3.6/site-packages/xarray/plot/plot.py in __call__(self, **kwargs)
    463 
    464     def __call__(self, **kwargs):
--> 465         return plot(self._da, **kwargs)
    466 
    467     @functools.wraps(hist)

~/python_venvs/python3/py3_18.04/lib/python3.6/site-packages/xarray/plot/plot.py in plot(darray, row, col, col_wrap, ax, hue, rtol, subplot_kws, **kwargs)
    200     kwargs["ax"] = ax
    201 
--> 202     return plotfunc(darray, **kwargs)
    203 
    204 

~/python_venvs/python3/py3_18.04/lib/python3.6/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)
    321         yplt_val = yplt.values
    322 
--> 323     _ensure_plottable(xplt_val, yplt_val)
    324 
    325     primitive = ax.plot(xplt_val, yplt_val, *args, **kwargs)

~/python_venvs/python3/py3_18.04/lib/python3.6/site-packages/xarray/plot/utils.py in _ensure_plottable(*args)
    510         ):
    511             raise TypeError(
--> 512                 "Plotting requires coordinates to be numeric "
    513                 "or dates of type np.datetime64, "
    514                 "datetime.datetime, cftime.datetime or "

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

Output of xr.show_versions() in the setup that is not working

INSTALLED VERSIONS ------------------ commit: None python: 3.6.7 (default, Oct 22 2018, 11:32:17) [GCC 8.2.0] python-bits: 64 OS: Linux OS-release: 4.15.0-74-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.2 libnetcdf: 4.4.1.1

xarray: 0.14.1
pandas: 0.25.3
numpy: 1.18.1
scipy: 1.2.0
netCDF4: 1.4.2
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.0.3.4
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 2.9.2
distributed: None
matplotlib: 3.1.2
cartopy: 0.17.0
seaborn: 0.9.0
numbagg: None
setuptools: 40.6.3
pip: 20.0.1
conda: None
pytest: None
IPython: 7.2.0
sphinx: None

Output of xr.show_versions() in the setup that was working

INSTALLED VERSIONS ------------------ commit: None python: 3.5.2.final.0 python-bits: 64 OS: Linux OS-release: 4.4.0-169-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8

xarray: 0.10.7
pandas: 0.22.0
numpy: 1.14.2
scipy: 1.0.0
netCDF4: 1.3.1
h5netcdf: None
h5py: 2.6.0
Nio: None
zarr: None
bottleneck: None
cyordereddict: None
dask: 1.1.3
distributed: None
matplotlib: 2.2.0
cartopy: 0.16.0
seaborn: 0.8.1
setuptools: 26.1.1
pip: 19.3.1
conda: None
pytest: None
IPython: 6.2.1

@dcherian
Copy link
Contributor

matplotlib can plot bool values so we should add that to the check in _ensure_plottable.

xarray/xarray/plot/utils.py

Lines 528 to 565 in ecd67f4

def _ensure_plottable(*args):
"""
Raise exception if there is anything in args that can't be plotted on an
axis by matplotlib.
"""
numpy_types = [np.floating, np.integer, np.timedelta64, np.datetime64]
other_types = [datetime]
try:
import cftime
cftime_datetime = [cftime.datetime]
except ImportError:
cftime_datetime = []
other_types = other_types + cftime_datetime
for x in args:
if not (
_valid_numpy_subdtype(np.array(x), numpy_types)
or _valid_other_type(np.array(x), other_types)
):
raise TypeError(
"Plotting requires coordinates to be numeric "
"or dates of type np.datetime64, "
"datetime.datetime, cftime.datetime or "
"pd.Interval."
)
if (
_valid_other_type(np.array(x), cftime_datetime)
and not nc_time_axis_available
):
raise ImportError(
"Plotting of arrays of cftime.datetime "
"objects or arrays indexed by "
"cftime.datetime objects requires the "
"optional `nc-time-axis` (v1.2.0 or later) "
"package."
)

This should be an easy fix if you are up for it.

A workaround would be to do boolean_da.astype(np.int).plot()

@MeraX
Copy link
Contributor Author

MeraX commented Jan 28, 2020

dcherian, I would appreciate that fix. That is exactly the solution that I use locally.

@dcherian
Copy link
Contributor

If you have a time, we would appreciate a PR. if not, no worries.

MeraX added a commit to MeraX/xarray that referenced this issue Feb 12, 2020
Fixes pydata#3722 .

> matplotlib can plot `bool` values so we should add that to the check in `_ensure_plottable`.
@MeraX MeraX mentioned this issue Feb 12, 2020
1 task
dcherian added a commit that referenced this issue Apr 3, 2020
* Allow plotting bool data

Fixes #3722 .

> matplotlib can plot `bool` values so we should add that to the check in `_ensure_plottable`.

* Add tests + raise nicer error when asked to plot unsupported types

* Add whats-new

Co-authored-by: dcherian <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants