From fb9adcbe0479469e9d96054a47d4ec6eab9aa86d Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Sat, 31 Aug 2019 04:24:59 -0400 Subject: [PATCH] Use drawstyle instead of linestyle in plot.step. Mixing the two is deprecated in Matplotlib 3.1, and breaks the doc build if warnings are set to errors (which they are in new IPython sphinx extensions.) --- doc/whats-new.rst | 7 ++++++- xarray/plot/plot.py | 18 +++++++++--------- xarray/plot/utils.py | 4 ++-- xarray/tests/test_plot.py | 4 ++++ 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index eebd04123d1..14941228c88 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -20,7 +20,12 @@ v0.16.0 (unreleased) Breaking changes ~~~~~~~~~~~~~~~~ - +- Alternate draw styles for :py:meth:`plot.step` must be passed using the + ``drawstyle`` (or ``ds``) keyword argument, instead of the ``linestyle`` (or + ``ls``) keyword argument, in line with the `upstream change in Matplotlib + `_. + (:pull:`3274`) + By `Elliott Sales de Andrade `_ New Features ~~~~~~~~~~~~ diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index 98131887e28..302cac05b05 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -329,7 +329,7 @@ def line( return primitive -def step(darray, *args, where="pre", linestyle=None, ls=None, **kwargs): +def step(darray, *args, where="pre", drawstyle=None, ds=None, **kwargs): """ Step plot of DataArray index against values @@ -359,16 +359,16 @@ def step(darray, *args, where="pre", linestyle=None, ls=None, **kwargs): if where not in {"pre", "post", "mid"}: raise ValueError("'where' argument to step must be " "'pre', 'post' or 'mid'") - if ls is not None: - if linestyle is None: - linestyle = ls + if ds is not None: + if drawstyle is None: + drawstyle = ds else: - raise TypeError("ls and linestyle are mutually exclusive") - if linestyle is None: - linestyle = "" - linestyle = "steps-" + where + linestyle + raise TypeError("ds and drawstyle are mutually exclusive") + if drawstyle is None: + drawstyle = "" + drawstyle = "steps-" + where + drawstyle - return line(darray, *args, linestyle=linestyle, **kwargs) + return line(darray, *args, drawstyle=drawstyle, **kwargs) def hist( diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index cb3bef6d409..e6c15037cb8 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -465,7 +465,7 @@ def _resolve_intervals_1dplot(xval, yval, xlabel, ylabel, kwargs): """ # Is it a step plot? (see matplotlib.Axes.step) - if kwargs.get("linestyle", "").startswith("steps-"): + if kwargs.get("drawstyle", "").startswith("steps-"): # Convert intervals to double points if _valid_other_type(np.array([xval, yval]), [pd.Interval]): @@ -476,7 +476,7 @@ def _resolve_intervals_1dplot(xval, yval, xlabel, ylabel, kwargs): yval, xval = _interval_to_double_bound_points(yval, xval) # Remove steps-* to be sure that matplotlib is not confused - del kwargs["linestyle"] + del kwargs["drawstyle"] # Is it another kind of plot? else: diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index c1549c62038..7f3f1620133 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -591,6 +591,10 @@ def setUp(self): def test_step(self): self.darray[0, 0].plot.step() + @pytest.mark.parametrize("ds", ["pre", "post", "mid"]) + def test_step_with_drawstyle(self, ds): + self.darray[0, 0].plot.step(drawstyle=ds) + def test_coord_with_interval_step(self): """Test step plot with intervals.""" bins = [-1, 0, 1, 2]