From 44b631c372ed8ef33f3b71e207f372e8333e5561 Mon Sep 17 00:00:00 2001 From: Thomas Nicholas Date: Tue, 24 Sep 2019 11:14:26 +0100 Subject: [PATCH 1/4] 1D dimension coords now cast down to dims --- xarray/plot/plot.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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] From e777a30f7091531b63c201d7119030aedeac4865 Mon Sep 17 00:00:00 2001 From: Thomas Nicholas Date: Wed, 25 Sep 2019 16:31:25 +0100 Subject: [PATCH 2/4] Added tests --- xarray/tests/test_plot.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 020a49b0114..2eb46bd5d13 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -170,6 +170,20 @@ 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() From 3c0195f751233e4641231369e3df8d4aea08d6f4 Mon Sep 17 00:00:00 2001 From: Thomas Nicholas Date: Wed, 25 Sep 2019 16:40:45 +0100 Subject: [PATCH 3/4] Updated what's new --- doc/whats-new.rst | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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 From 802014ab1897ff1d38328a748100c096c3af8a17 Mon Sep 17 00:00:00 2001 From: Thomas Nicholas Date: Wed, 25 Sep 2019 16:54:41 +0100 Subject: [PATCH 4/4] Applied black formatting --- xarray/tests/test_plot.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 2eb46bd5d13..5ce75ad2396 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -172,16 +172,19 @@ def test_infer_line_data(self): 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']) + 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}) + 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] + 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] + line = da.plot(y="time", hue="x")[0] assert_array_equal(line.get_ydata(), da.coords["time"].values) def test_2d_line(self):