Skip to content

Commit

Permalink
improve the error message for invalid hue names (pydata#5060)
Browse files Browse the repository at this point in the history
  • Loading branch information
keewis authored Mar 30, 2021
1 parent 483f751 commit 821479d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions xarray/plot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def _infer_line_data(darray, x, y, hue):
raise ValueError("For 2D inputs, please specify either hue, x or y.")

if y is None:
if hue is not None:
_assert_valid_xy(darray, hue, "hue")
xname, huename = _infer_xy_labels(darray=darray, x=x, y=hue)
xplt = darray[xname]
if xplt.ndim > 1:
Expand Down
9 changes: 9 additions & 0 deletions xarray/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,15 @@ def test_line_plot_along_1d_coord(self):
line = da.plot(y="time", hue="x")[0]
assert_array_equal(line.get_ydata(), da.coords["time"].values)

def test_line_plot_wrong_hue(self):
da = xr.DataArray(
data=np.array([[0, 1], [5, 9]]),
dims=["x", "t"],
)

with pytest.raises(ValueError, match="hue must be one of"):
da.plot(x="t", hue="wrong_coord")

def test_2d_line(self):
with raises_regex(ValueError, "hue"):
self.darray[:, :, 0].plot.line()
Expand Down

0 comments on commit 821479d

Please sign in to comment.