diff --git a/doc/whats-new.rst b/doc/whats-new.rst index d8d3382675e..828a66db6ab 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -24,6 +24,9 @@ Bug fixes reinstated for :class:`DataArray` and :class:`Dataset` objects only. Internal xarray objects remain unaddressable by weakref in order to save memory. (:issue:`3317`) by `Guido Imperiale `_. +- Line plots with the ``x`` or ``y`` argument set to a 1D non-dimensional coord + now plot the correct data for 2D DataArrays. + (:issue:`3334`). By `Tom Nicholas `_. Documentation ~~~~~~~~~~~~~ @@ -544,8 +547,9 @@ Bug fixes from higher frequencies to lower frequencies. Datapoints outside the bounds of the original time coordinate are now filled with NaN (:issue:`2197`). By `Spencer Clark `_. -- Line plots with the ``x`` argument set to a non-dimensional coord now plot the correct data for 1D DataArrays. - (:issue:`27251`). By `Tom Nicholas `_. +- Line plots with the ``x`` argument set to a non-dimensional coord now plot + the correct data for 1D DataArrays. + (:issue:`2725`). By `Tom Nicholas `_. - Subtracting a scalar ``cftime.datetime`` object from a :py:class:`CFTimeIndex` now results in a :py:class:`pandas.TimedeltaIndex` instead of raising a ``TypeError`` (:issue:`2671`). By `Spencer Clark diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index 8ca62ef58f1..7938f9b027b 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -83,7 +83,9 @@ def _infer_line_data(darray, x, y, hue): ) else: - yplt = darray.transpose(xname, huename) + xdim, = darray[xname].dims + huedim, = darray[huename].dims + yplt = darray.transpose(xdim, huedim) else: yname, huename = _infer_xy_labels(darray=darray, x=y, y=hue) @@ -100,7 +102,9 @@ def _infer_line_data(darray, x, y, hue): ) else: - xplt = darray.transpose(yname, huename) + ydim, = darray[yname].dims + huedim, = darray[huename].dims + xplt = darray.transpose(ydim, huedim) huelabel = label_from_attrs(darray[huename]) hueplt = darray[huename] diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 020a49b0114..5ce75ad2396 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -170,6 +170,23 @@ def test_infer_line_data(self): line = current.plot.line()[0] assert_array_equal(line.get_xdata(), current.coords["t"].values) + def test_line_plot_along_1d_coord(self): + # Test for bug in GH #3334 + x_coord = xr.DataArray(data=[0.1, 0.2], dims=["x"]) + t_coord = xr.DataArray(data=[10, 20], dims=["t"]) + + da = xr.DataArray( + data=np.array([[0, 1], [5, 9]]), + dims=["x", "t"], + coords={"x": x_coord, "time": t_coord}, + ) + + line = da.plot(x="time", hue="x")[0] + assert_array_equal(line.get_xdata(), da.coords["time"].values) + + line = da.plot(y="time", hue="x")[0] + assert_array_equal(line.get_ydata(), da.coords["time"].values) + def test_2d_line(self): with raises_regex(ValueError, "hue"): self.darray[:, :, 0].plot.line()