Skip to content

Commit 4ac7f2d

Browse files
authored
Fix step plots (#4508)
1 parent d7f445e commit 4ac7f2d

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

doc/whats-new.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ New Features
3636
Bug fixes
3737
~~~~~~~~~
3838

39+
- Fix :py:meth:`DataArray.plot.step`. By `Deepak Cherian <https://github.com/dcherian>`_.
3940
- Fix bug where reading a scalar value from a NetCDF file opened with the ``h5netcdf`` backend would raise a ``ValueError`` when ``decode_cf=True`` (:issue:`4471`, :pull:`4485`).
4041
By `Gerrit Holl <https://github.com/gerritholl>`_.
4142
- Fix bug where datetime64 times are silently changed to incorrect values if they are outside the valid date range for ns precision when provided in some other units (:issue:`4427`, :pull:`4454`).

xarray/plot/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,16 +513,20 @@ def _resolve_intervals_1dplot(xval, yval, xlabel, ylabel, kwargs):
513513
# Is it a step plot? (see matplotlib.Axes.step)
514514
if kwargs.get("drawstyle", "").startswith("steps-"):
515515

516+
remove_drawstyle = False
516517
# Convert intervals to double points
517518
if _valid_other_type(np.array([xval, yval]), [pd.Interval]):
518519
raise TypeError("Can't step plot intervals against intervals.")
519520
if _valid_other_type(xval, [pd.Interval]):
520521
xval, yval = _interval_to_double_bound_points(xval, yval)
522+
remove_drawstyle = True
521523
if _valid_other_type(yval, [pd.Interval]):
522524
yval, xval = _interval_to_double_bound_points(yval, xval)
525+
remove_drawstyle = True
523526

524527
# Remove steps-* to be sure that matplotlib is not confused
525-
del kwargs["drawstyle"]
528+
if remove_drawstyle:
529+
del kwargs["drawstyle"]
526530

527531
# Is it another kind of plot?
528532
else:

xarray/tests/test_plot.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -670,11 +670,13 @@ def setUp(self):
670670
self.darray = DataArray(easy_array((2, 3, 4)))
671671

672672
def test_step(self):
673-
self.darray[0, 0].plot.step()
673+
hdl = self.darray[0, 0].plot.step()
674+
assert "steps" in hdl[0].get_drawstyle()
674675

675-
@pytest.mark.parametrize("ds", ["pre", "post", "mid"])
676-
def test_step_with_drawstyle(self, ds):
677-
self.darray[0, 0].plot.step(drawstyle=ds)
676+
@pytest.mark.parametrize("where", ["pre", "post", "mid"])
677+
def test_step_with_where(self, where):
678+
hdl = self.darray[0, 0].plot.step(where=where)
679+
assert hdl[0].get_drawstyle() == f"steps-{where}"
678680

679681
def test_coord_with_interval_step(self):
680682
"""Test step plot with intervals."""

0 commit comments

Comments
 (0)