From c2bbdf2ae9cef7e3b398652f30a8b43cfb35c1c8 Mon Sep 17 00:00:00 2001 From: mgunyho Date: Sun, 21 Aug 2022 23:12:17 +0300 Subject: [PATCH 1/5] Add failing tests for step plot with hue --- xarray/tests/test_plot.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 08bf6af8a66..bf3ca6e29bb 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -796,6 +796,24 @@ def test_step_with_where(self, where): hdl = self.darray[0, 0].plot.step(where=where) assert hdl[0].get_drawstyle() == f"steps-{where}" + def test_step_with_hue(self): + hdl = self.darray[0].plot.step(hue="dim_2") + assert hdl[0].get_drawstyle() == "steps" + + @pytest.mark.parametrize("where", ["pre", "post", "mid"]) + def test_step_with_hue_and_where(self, where): + hdl = self.darray[0].plot.step(hue="dim_2", where=where) + assert hdl[0].get_drawstyle() == f"steps-{where}" + + def test_drawstyle_steps(self): + hdl = self.darray[0].plot(hue="dim_2", drawstyle="steps") + assert hdl[0].get_drawstyle() == "steps" + + @pytest.mark.parametrize("where", ["pre", "post", "mid"]) + def test_drawstyle_steps_with_where(self, where): + hdl = self.darray[0].plot(hue="dim_2", drawstyle=f"steps-{where}") + assert hdl[0].get_drawstyle() == f"steps-{where}" + def test_coord_with_interval_step(self): """Test step plot with intervals.""" bins = [-1, 0, 1, 2] From a6d4b5eedbae8f06043109744af6839a8fb85dbe Mon Sep 17 00:00:00 2001 From: mgunyho Date: Mon, 22 Aug 2022 07:32:58 +0300 Subject: [PATCH 2/5] Add another test for interval on both axes --- xarray/tests/test_plot.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index bf3ca6e29bb..f75cb9732aa 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -832,6 +832,15 @@ def test_coord_with_interval_step_y(self): self.darray.groupby_bins("dim_0", bins).mean(...).plot.step(y="dim_0_bins") assert len(plt.gca().lines[0].get_xdata()) == ((len(bins) - 1) * 2) + def test_coord_with_interval_step_x_and_y_raises_valueeerror(self): + """Test that step plot with intervals both on x and y axes raises an error.""" + arr = xr.DataArray( + [pd.Interval(0, 1), pd.Interval(1, 2)], + coords=[("x", [pd.Interval(0, 1), pd.Interval(1, 2)])], + ) + with pytest.raises(TypeError, match="intervals against intervals"): + arr.plot.step() + class TestPlotHistogram(PlotTestCase): @pytest.fixture(autouse=True) From 8912d5146bfba2cec06c94ade98c2fc0a4180bc3 Mon Sep 17 00:00:00 2001 From: mgunyho Date: Mon, 22 Aug 2022 07:39:32 +0300 Subject: [PATCH 3/5] Fix shape error in step plot type check --- xarray/plot/utils.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index f004a2645c9..f106d56689c 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -564,13 +564,16 @@ def _resolve_intervals_1dplot( if kwargs.get("drawstyle", "").startswith("steps-"): remove_drawstyle = False + # Convert intervals to double points - if _valid_other_type(np.array([xval, yval]), [pd.Interval]): + x_is_interval = _valid_other_type(xval, [pd.Interval]) + y_is_interval = _valid_other_type(yval, [pd.Interval]) + if x_is_interval and y_is_interval: raise TypeError("Can't step plot intervals against intervals.") - if _valid_other_type(xval, [pd.Interval]): + elif x_is_interval: xval, yval = _interval_to_double_bound_points(xval, yval) remove_drawstyle = True - if _valid_other_type(yval, [pd.Interval]): + elif y_is_interval: yval, xval = _interval_to_double_bound_points(yval, xval) remove_drawstyle = True From 87f19638afbc68665bda2184dfc82a54ec324c20 Mon Sep 17 00:00:00 2001 From: mgunyho Date: Mon, 22 Aug 2022 07:39:40 +0300 Subject: [PATCH 4/5] Fix test --- 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 f75cb9732aa..f37c2fd7508 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -798,7 +798,7 @@ def test_step_with_where(self, where): def test_step_with_hue(self): hdl = self.darray[0].plot.step(hue="dim_2") - assert hdl[0].get_drawstyle() == "steps" + assert hdl[0].get_drawstyle() == "steps-pre" @pytest.mark.parametrize("where", ["pre", "post", "mid"]) def test_step_with_hue_and_where(self, where): From 695235b8d1e54484a6e1c829bbb9205723b5b1eb Mon Sep 17 00:00:00 2001 From: mgunyho Date: Mon, 22 Aug 2022 08:09:52 +0300 Subject: [PATCH 5/5] Update what's new --- doc/whats-new.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 079c5f51c95..d77422df5b4 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -53,6 +53,8 @@ Bug fixes By `Michael Niklas `_. - Harmonize returned multi-indexed indexes when applying ``concat`` along new dimension (:issue:`6881`, :pull:`6889`) By `Fabian Hofmann `_. +- Fix step plots with ``hue`` arg. (:pull:`6944`) + By `András Gunyhó `_. Documentation ~~~~~~~~~~~~~