diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 512cd40defb..5e890022e36 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -59,6 +59,9 @@ Bug fixes By `Todd Jennings `_ - Fix ``FacetGrid`` when ``vmin == vmax``. (:issue:`3734`) By `Deepak Cherian `_ +- Fix bug where plotting line plots with 2D coordinates depended on dimension + order. (:issue:`3933`) + By `Tom Nicholas `_. Documentation ~~~~~~~~~~~~~ diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index 71a05627692..0fe302833d4 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -93,6 +93,7 @@ def _infer_line_data(darray, x, y, hue): otherindex = 1 if darray.dims.index(huename) == 0 else 0 otherdim = darray.dims[otherindex] xplt = darray.transpose(otherdim, huename, transpose_coords=False) + yplt = yplt.transpose(otherdim, huename, transpose_coords=False) else: raise ValueError( "For 2D inputs, hue must be a dimension" diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 02eaf09b73e..6482aa2400c 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -257,6 +257,17 @@ def test_2d_coords_line_plot(self): with pytest.raises(ValueError, match="For 2D inputs, hue must be a dimension"): da.plot.line(x="lon", hue="lat") + def test_2d_coord_line_plot_coords_transpose_invariant(self): + # checks for bug reported in GH #3933 + x = np.arange(10) + y = np.arange(20) + ds = xr.Dataset(coords={"x": x, "y": y}) + + for z in [ds.y + ds.x, ds.x + ds.y]: + ds = ds.assign_coords(z=z) + ds["v"] = ds.x + ds.y + ds["v"].plot.line(y="z", hue="x") + def test_2d_before_squeeze(self): a = DataArray(easy_array((1, 5))) a.plot()