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

Allow plotting bool data #3766

Merged
merged 4 commits into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ New Features
- Implement :py:meth:`DataArray.idxmax`, :py:meth:`DataArray.idxmin`,
:py:meth:`Dataset.idxmax`, :py:meth:`Dataset.idxmin`. (:issue:`60`, :pull:`3871`)
By `Todd Jennings <https://github.com/toddrjen>`_

- Allow plotting of boolean arrays. (:pull:`3766`)
By `Marek Jacob <https://github.com/MeraX>`_

Bug fixes
~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion xarray/plot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ def newplotfunc(
xplt, xlab_extra = _resolve_intervals_2dplot(xval, plotfunc.__name__)
yplt, ylab_extra = _resolve_intervals_2dplot(yval, plotfunc.__name__)

_ensure_plottable(xplt, yplt)
_ensure_plottable(xplt, yplt, zval)

cmap_params, cbar_kwargs = _process_cmap_cbar_kwargs(
plotfunc, zval.data, **locals()
Expand Down
8 changes: 4 additions & 4 deletions xarray/plot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ 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]
numpy_types = [np.floating, np.integer, np.timedelta64, np.datetime64, np.bool_]
other_types = [datetime]
try:
import cftime
Expand All @@ -549,10 +549,10 @@ def _ensure_plottable(*args):
or _valid_other_type(np.array(x), other_types)
):
raise TypeError(
"Plotting requires coordinates to be numeric "
"or dates of type np.datetime64, "
"Plotting requires coordinates to be numeric, boolean, "
"or dates of type numpy.datetime64, "
"datetime.datetime, cftime.datetime or "
"pd.Interval."
f"pandas.Interval. Received data of type {np.array(x).dtype} instead."
)
if (
_valid_other_type(np.array(x), cftime_datetime)
Expand Down
13 changes: 13 additions & 0 deletions xarray/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ def test1d(self):
with raises_regex(ValueError, "None"):
self.darray[:, 0, 0].plot(x="dim_1")

with raises_regex(TypeError, "complex128"):
(self.darray[:, 0, 0] + 1j).plot()

def test_1d_bool(self):
xr.ones_like(self.darray[:, 0, 0], dtype=np.bool).plot()

def test_1d_x_y_kw(self):
z = np.arange(10)
da = DataArray(np.cos(z), dims=["z"], coords=[z], name="f")
Expand Down Expand Up @@ -989,6 +995,13 @@ def test_1d_raises_valueerror(self):
with raises_regex(ValueError, r"DataArray must be 2d"):
self.plotfunc(self.darray[0, :])

def test_bool(self):
xr.ones_like(self.darray, dtype=np.bool).plot()

def test_complex_raises_typeerror(self):
with raises_regex(TypeError, "complex128"):
(self.darray + 1j).plot()

def test_3d_raises_valueerror(self):
a = DataArray(easy_array((2, 3, 4)))
if self.plotfunc.__name__ == "imshow":
Expand Down