From f7e508423e715c53faf04ed6593f936cb6ceac82 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sat, 12 Jun 2021 17:51:08 +0200 Subject: [PATCH 01/18] Allow plotting categorical data --- xarray/plot/plot.py | 16 ++++++++++++---- xarray/plot/utils.py | 9 ++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index f530427562a..94ab90ebda1 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -1011,9 +1011,13 @@ def pcolormesh(x, y, z, ax, infer_intervals=None, **kwargs): else: infer_intervals = True - if infer_intervals and ( - (np.shape(x)[0] == np.shape(z)[1]) - or ((x.ndim > 1) and (np.shape(x)[1] == np.shape(z)[1])) + if ( + infer_intervals + and not np.issubdtype(x.dtype, str) + and ( + (np.shape(x)[0] == np.shape(z)[1]) + or ((x.ndim > 1) and (np.shape(x)[1] == np.shape(z)[1])) + ) ): if len(x.shape) == 1: x = _infer_interval_breaks(x, check_monotonic=True) @@ -1022,7 +1026,11 @@ def pcolormesh(x, y, z, ax, infer_intervals=None, **kwargs): x = _infer_interval_breaks(x, axis=1) x = _infer_interval_breaks(x, axis=0) - if infer_intervals and (np.shape(y)[0] == np.shape(z)[0]): + if ( + infer_intervals + and not np.issubdtype(y.dtype, str) + and (np.shape(y)[0] == np.shape(z)[0]) + ): if len(y.shape) == 1: y = _infer_interval_breaks(y, check_monotonic=True) else: diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index 416d56aa620..db85a5908c0 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -604,7 +604,14 @@ 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, np.bool_] + numpy_types = [ + np.floating, + np.integer, + np.timedelta64, + np.datetime64, + np.bool_, + np.str_, + ] other_types = [datetime] try: import cftime From 0a6cce93f9a602348f761100acd7d018fdd85c2d Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sat, 12 Jun 2021 18:08:58 +0200 Subject: [PATCH 02/18] change tests --- xarray/tests/test_plot.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index bbd7c31fa16..afbd228b9aa 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -684,10 +684,9 @@ def test_format_string(self): def test_can_pass_in_axis(self): self.pass_in_axis(self.darray.plot.line) - def test_nonnumeric_index_raises_typeerror(self): + def test_nonnumeric_index(self): a = DataArray([1, 2, 3], {"letter": ["a", "b", "c"]}, dims="letter") - with pytest.raises(TypeError, match=r"[Pp]lot"): - a.plot.line() + a.plot.line() def test_primitive_returned(self): p = self.darray.plot.line() @@ -1162,10 +1161,9 @@ def test_3d_raises_valueerror(self): with pytest.raises(ValueError, match=r"DataArray must be 2d"): self.plotfunc(a) - def test_nonnumeric_index_raises_typeerror(self): + def test_nonnumeric_index(self): a = DataArray(easy_array((3, 2)), coords=[["a", "b", "c"], ["d", "e"]]) - with pytest.raises(TypeError, match=r"[Pp]lot"): - self.plotfunc(a) + self.plotfunc(a) def test_multiindex_raises_typeerror(self): a = DataArray( From 2ccfa6bd735a2244e9592d42581f4d36925e586c Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sat, 12 Jun 2021 18:50:31 +0200 Subject: [PATCH 03/18] Update plot.py --- xarray/plot/plot.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index 94ab90ebda1..8859704ead0 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -925,18 +925,20 @@ def imshow(x, y, z, ax, **kwargs): "imshow requires 1D coordinates, try using pcolormesh or contour(f)" ) + def _center_pixels(x): + if np.issubclass(x.dtype, str): + return x[0], x[-1] + + try: + xstep = (x[1] - x[0]) / 2.0 + except IndexError: + # Arbitrary default value, similar to matplotlib behaviour + xstep = 0.1 + return x[0] - xstep, x[-1] + xstep + # Centering the pixels- Assumes uniform spacing - try: - xstep = (x[1] - x[0]) / 2.0 - except IndexError: - # Arbitrary default value, similar to matplotlib behaviour - xstep = 0.1 - try: - ystep = (y[1] - y[0]) / 2.0 - except IndexError: - ystep = 0.1 - left, right = x[0] - xstep, x[-1] + xstep - bottom, top = y[-1] + ystep, y[0] - ystep + left, right = _center_pixels(x) + top, bottom = _center_pixels(y) defaults = {"origin": "upper", "interpolation": "nearest"} From 21c028fb2fba54ea9bcd95937e28b56b427584e0 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sat, 12 Jun 2021 19:05:03 +0200 Subject: [PATCH 04/18] Update plot.py --- xarray/plot/plot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index 8859704ead0..8e610b153be 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -926,7 +926,7 @@ def imshow(x, y, z, ax, **kwargs): ) def _center_pixels(x): - if np.issubclass(x.dtype, str): + if np.issubdtype(x.dtype, str): return x[0], x[-1] try: From a393e9182791f3fd6253a135717185fda2fd0672 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sat, 12 Jun 2021 19:34:00 +0200 Subject: [PATCH 05/18] Update plot.py --- xarray/plot/plot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index 8e610b153be..d889e6c7dd3 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -927,7 +927,7 @@ def imshow(x, y, z, ax, **kwargs): def _center_pixels(x): if np.issubdtype(x.dtype, str): - return x[0], x[-1] + return None, None try: xstep = (x[1] - x[0]) / 2.0 From eb96ee9717b0b97a4a516bd45d12ab84d5b26e88 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sat, 12 Jun 2021 20:47:09 +0200 Subject: [PATCH 06/18] ismhow and plot_surface doesn't support cats --- xarray/plot/plot.py | 5 +++++ xarray/tests/test_plot.py | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index d889e6c7dd3..b7347d62c37 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -919,6 +919,8 @@ def imshow(x, y, z, ax, **kwargs): The pixels are centered on the coordinates. For example, if the coordinate value is 3.2, then the pixels for those coordinates will be centered on 3.2. """ + if any(np.issubdtyp(v.dtype, str) for v in [x, y]): + raise TypeError("ax.imshow does not support categorical data.") if x.ndim != 1 or y.ndim != 1: raise ValueError( @@ -1059,5 +1061,8 @@ def surface(x, y, z, ax, **kwargs): Wraps :py:meth:`matplotlib:mpl_toolkits.mplot3d.axes3d.Axes3D.plot_surface`. """ + if any(np.issubdtyp(v.dtype, str) for v in [x, y]): + raise TypeError("ax.plot_surface does not support categorical data.") + primitive = ax.plot_surface(x, y, z, **kwargs) return primitive diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index afbd228b9aa..436446edbf2 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -1163,7 +1163,11 @@ def test_3d_raises_valueerror(self): def test_nonnumeric_index(self): a = DataArray(easy_array((3, 2)), coords=[["a", "b", "c"], ["d", "e"]]) - self.plotfunc(a) + if self.plotfunc.__name__ in ["imshow", "surface"]: + with pytest.raises(TypeError, match=r"does not support categorical data"): + self.plotfunc(a) + else: + self.plotfunc(a) def test_multiindex_raises_typeerror(self): a = DataArray( From f6ff2d21dc4d3c35715443e6d537ba0c69da3579 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sat, 12 Jun 2021 23:31:02 +0200 Subject: [PATCH 07/18] Update plot.py --- xarray/plot/plot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index b7347d62c37..92fb0b61ee7 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -919,7 +919,7 @@ def imshow(x, y, z, ax, **kwargs): The pixels are centered on the coordinates. For example, if the coordinate value is 3.2, then the pixels for those coordinates will be centered on 3.2. """ - if any(np.issubdtyp(v.dtype, str) for v in [x, y]): + if any(np.issubdtype(v.dtype, str) for v in [x, y]): raise TypeError("ax.imshow does not support categorical data.") if x.ndim != 1 or y.ndim != 1: @@ -1061,7 +1061,7 @@ def surface(x, y, z, ax, **kwargs): Wraps :py:meth:`matplotlib:mpl_toolkits.mplot3d.axes3d.Axes3D.plot_surface`. """ - if any(np.issubdtyp(v.dtype, str) for v in [x, y]): + if any(np.issubdtype(v.dtype, str) for v in [x, y]): raise TypeError("ax.plot_surface does not support categorical data.") primitive = ax.plot_surface(x, y, z, **kwargs) From bd9f9bafb78a1c15762d7ab1d7e0b1fbe62490d4 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sun, 13 Jun 2021 06:36:50 +0200 Subject: [PATCH 08/18] Update plot.py --- xarray/plot/plot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index 92fb0b61ee7..510f0e985e7 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -932,7 +932,7 @@ def _center_pixels(x): return None, None try: - xstep = (x[1] - x[0]) / 2.0 + xstep = 0.5 * (x[1] - x[0]) except IndexError: # Arbitrary default value, similar to matplotlib behaviour xstep = 0.1 From c346c2567e92bfb9f571e23a50b236855099f9a1 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sun, 13 Jun 2021 11:48:37 +0200 Subject: [PATCH 09/18] Better comments --- xarray/plot/plot.py | 13 ++++++++----- xarray/tests/test_plot.py | 1 + 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index 510f0e985e7..37e9a1c40d9 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -927,20 +927,23 @@ def imshow(x, y, z, ax, **kwargs): "imshow requires 1D coordinates, try using pcolormesh or contour(f)" ) - def _center_pixels(x): + def _maybe_center_pixels(x): + """Center the pixels on the coordinates.""" if np.issubdtype(x.dtype, str): + # Categorical data cannot be centered: return None, None try: + # Center the pixels assuming uniform spacing: xstep = 0.5 * (x[1] - x[0]) except IndexError: - # Arbitrary default value, similar to matplotlib behaviour + # Arbitrary default value, similar to matplotlib behaviour: xstep = 0.1 return x[0] - xstep, x[-1] + xstep - # Centering the pixels- Assumes uniform spacing - left, right = _center_pixels(x) - top, bottom = _center_pixels(y) + # Try to center the pixels: + left, right = _maybe_center_pixels(x) + top, bottom = _maybe_center_pixels(y) defaults = {"origin": "upper", "interpolation": "nearest"} diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 436446edbf2..2252b9ff060 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -1164,6 +1164,7 @@ def test_3d_raises_valueerror(self): def test_nonnumeric_index(self): a = DataArray(easy_array((3, 2)), coords=[["a", "b", "c"], ["d", "e"]]) if self.plotfunc.__name__ in ["imshow", "surface"]: + # ax.imshow and ax.plot_surface errors with nonnumerics: with pytest.raises(TypeError, match=r"does not support categorical data"): self.plotfunc(a) else: From f922ce3c66d65705dbf395e716285ad22d60bf2b Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Mon, 14 Jun 2021 19:45:24 +0200 Subject: [PATCH 10/18] Update whats-new.rst --- doc/whats-new.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index ff67ea20073..355e19ee2a8 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -38,6 +38,8 @@ New Features By `Thomas Hirtz `_. - allow passing a function to ``combine_attrs`` (:pull:`4896`). By `Justus Magin `_. +- Allow plotting categorical data (:pull:`5464`). + By `Jimmy Westling `_. Breaking changes ~~~~~~~~~~~~~~~~ From 3a1c196401915a63733c45d1916af3656b7a71ee Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Wed, 16 Jun 2021 18:19:20 +0200 Subject: [PATCH 11/18] Don't handle pixel centering when inputs are strings. Has to be refixed if/when ax.imshow allows categorical data. --- xarray/plot/plot.py | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index 37e9a1c40d9..e4926395f84 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -927,23 +927,18 @@ def imshow(x, y, z, ax, **kwargs): "imshow requires 1D coordinates, try using pcolormesh or contour(f)" ) - def _maybe_center_pixels(x): - """Center the pixels on the coordinates.""" - if np.issubdtype(x.dtype, str): - # Categorical data cannot be centered: - return None, None - - try: - # Center the pixels assuming uniform spacing: - xstep = 0.5 * (x[1] - x[0]) - except IndexError: - # Arbitrary default value, similar to matplotlib behaviour: - xstep = 0.1 - return x[0] - xstep, x[-1] + xstep - - # Try to center the pixels: - left, right = _maybe_center_pixels(x) - top, bottom = _maybe_center_pixels(y) + # Centering the pixels- Assumes uniform spacing + try: + xstep = (x[1] - x[0]) / 2.0 + except IndexError: + # Arbitrary default value, similar to matplotlib behaviour + xstep = 0.1 + try: + ystep = (y[1] - y[0]) / 2.0 + except IndexError: + ystep = 0.1 + left, right = x[0] - xstep, x[-1] + xstep + bottom, top = y[-1] + ystep, y[0] - ystep defaults = {"origin": "upper", "interpolation": "nearest"} From c22b3be35c1f78719a4052a57b75192815874037 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Wed, 16 Jun 2021 18:43:42 +0200 Subject: [PATCH 12/18] Revert "Don't handle pixel centering when inputs are strings." This reverts commit 3a1c196401915a63733c45d1916af3656b7a71ee. --- xarray/plot/plot.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index e4926395f84..37e9a1c40d9 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -927,18 +927,23 @@ def imshow(x, y, z, ax, **kwargs): "imshow requires 1D coordinates, try using pcolormesh or contour(f)" ) - # Centering the pixels- Assumes uniform spacing - try: - xstep = (x[1] - x[0]) / 2.0 - except IndexError: - # Arbitrary default value, similar to matplotlib behaviour - xstep = 0.1 - try: - ystep = (y[1] - y[0]) / 2.0 - except IndexError: - ystep = 0.1 - left, right = x[0] - xstep, x[-1] + xstep - bottom, top = y[-1] + ystep, y[0] - ystep + def _maybe_center_pixels(x): + """Center the pixels on the coordinates.""" + if np.issubdtype(x.dtype, str): + # Categorical data cannot be centered: + return None, None + + try: + # Center the pixels assuming uniform spacing: + xstep = 0.5 * (x[1] - x[0]) + except IndexError: + # Arbitrary default value, similar to matplotlib behaviour: + xstep = 0.1 + return x[0] - xstep, x[-1] + xstep + + # Try to center the pixels: + left, right = _maybe_center_pixels(x) + top, bottom = _maybe_center_pixels(y) defaults = {"origin": "upper", "interpolation": "nearest"} From a9e382657b5b5a1cbdb8e5bdc093879c73ab3544 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Wed, 16 Jun 2021 18:47:48 +0200 Subject: [PATCH 13/18] Assume matplotlib handles the categoricals. --- xarray/plot/plot.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index 37e9a1c40d9..db4e1b32a64 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -919,9 +919,6 @@ def imshow(x, y, z, ax, **kwargs): The pixels are centered on the coordinates. For example, if the coordinate value is 3.2, then the pixels for those coordinates will be centered on 3.2. """ - if any(np.issubdtype(v.dtype, str) for v in [x, y]): - raise TypeError("ax.imshow does not support categorical data.") - if x.ndim != 1 or y.ndim != 1: raise ValueError( "imshow requires 1D coordinates, try using pcolormesh or contour(f)" @@ -1064,8 +1061,5 @@ def surface(x, y, z, ax, **kwargs): Wraps :py:meth:`matplotlib:mpl_toolkits.mplot3d.axes3d.Axes3D.plot_surface`. """ - if any(np.issubdtype(v.dtype, str) for v in [x, y]): - raise TypeError("ax.plot_surface does not support categorical data.") - primitive = ax.plot_surface(x, y, z, **kwargs) return primitive From e1c45fdcca53b7cc626af3b04634d4827a55a0b7 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Wed, 16 Jun 2021 18:51:54 +0200 Subject: [PATCH 14/18] Update plot.py --- xarray/plot/plot.py | 1 + 1 file changed, 1 insertion(+) diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index db4e1b32a64..e111a503dc9 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -919,6 +919,7 @@ def imshow(x, y, z, ax, **kwargs): The pixels are centered on the coordinates. For example, if the coordinate value is 3.2, then the pixels for those coordinates will be centered on 3.2. """ + if x.ndim != 1 or y.ndim != 1: raise ValueError( "imshow requires 1D coordinates, try using pcolormesh or contour(f)" From 3e6799bfa10e8a3cf7d35e80212f1dc105995963 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Wed, 16 Jun 2021 23:40:58 +0200 Subject: [PATCH 15/18] imshow has some support for strings. --- xarray/plot/plot.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index e111a503dc9..10ebcc07664 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -925,11 +925,13 @@ def imshow(x, y, z, ax, **kwargs): "imshow requires 1D coordinates, try using pcolormesh or contour(f)" ) - def _maybe_center_pixels(x): + def _center_pixels(x): """Center the pixels on the coordinates.""" if np.issubdtype(x.dtype, str): - # Categorical data cannot be centered: - return None, None + # When using strings as inputs imshow converts it to + # integers. Choose extent values which puts the indices in + # in the center of the pixels: + return 0 - 0.5, len(x) - 0.5 try: # Center the pixels assuming uniform spacing: @@ -937,11 +939,12 @@ def _maybe_center_pixels(x): except IndexError: # Arbitrary default value, similar to matplotlib behaviour: xstep = 0.1 + return x[0] - xstep, x[-1] + xstep - # Try to center the pixels: - left, right = _maybe_center_pixels(x) - top, bottom = _maybe_center_pixels(y) + # Center the pixels: + left, right = _center_pixels(x) + top, bottom = _center_pixels(y) defaults = {"origin": "upper", "interpolation": "nearest"} @@ -972,6 +975,13 @@ def _maybe_center_pixels(x): primitive = ax.imshow(z, **defaults) + # If x or y are strings the ticklabels have been replaced with + # integer indices. Replace them back to strings: + for axis, v in [("x", x), ("y", y)]: + if np.issubdtype(v.dtype, str): + getattr(ax, f"set_{axis}ticks")(np.arange(len(v))) + getattr(ax, f"set_{axis}ticklabels")(v) + return primitive From 1dc00c1f370765417a83eaa9cdd0e505f10251f4 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Wed, 16 Jun 2021 23:42:24 +0200 Subject: [PATCH 16/18] imshow test doesn't need to raise anymore --- xarray/tests/test_plot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 2252b9ff060..c6f5afafd52 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -1163,7 +1163,7 @@ def test_3d_raises_valueerror(self): def test_nonnumeric_index(self): a = DataArray(easy_array((3, 2)), coords=[["a", "b", "c"], ["d", "e"]]) - if self.plotfunc.__name__ in ["imshow", "surface"]: + if self.plotfunc.__name__ == "surface": # ax.imshow and ax.plot_surface errors with nonnumerics: with pytest.raises(TypeError, match=r"does not support categorical data"): self.plotfunc(a) From f08f22dfaa959bbe2ec7001e7d88c919426f9318 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Wed, 16 Jun 2021 23:59:11 +0200 Subject: [PATCH 17/18] catch plot_surface error --- xarray/tests/test_plot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index c6f5afafd52..d59d4015385 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -1164,8 +1164,8 @@ def test_3d_raises_valueerror(self): def test_nonnumeric_index(self): a = DataArray(easy_array((3, 2)), coords=[["a", "b", "c"], ["d", "e"]]) if self.plotfunc.__name__ == "surface": - # ax.imshow and ax.plot_surface errors with nonnumerics: - with pytest.raises(TypeError, match=r"does not support categorical data"): + # ax.plot_surface errors with nonnumerics: + with pytest.raises(np.core._exceptions._UFuncNoLoopError): self.plotfunc(a) else: self.plotfunc(a) From 43d669638d19f66703cdeda39bc7968cb6b7965e Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Thu, 17 Jun 2021 20:08:22 +0200 Subject: [PATCH 18/18] Update xarray/tests/test_plot.py Co-authored-by: Deepak Cherian --- xarray/tests/test_plot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index d59d4015385..e833654138a 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -1165,7 +1165,7 @@ def test_nonnumeric_index(self): a = DataArray(easy_array((3, 2)), coords=[["a", "b", "c"], ["d", "e"]]) if self.plotfunc.__name__ == "surface": # ax.plot_surface errors with nonnumerics: - with pytest.raises(np.core._exceptions._UFuncNoLoopError): + with pytest.raises(Exception): self.plotfunc(a) else: self.plotfunc(a)