Skip to content

Commit

Permalink
Bugfix/plot accept coord dim (#3345)
Browse files Browse the repository at this point in the history
Bug in plot.line fixed by ensuring 1D coords are cast down to their associated dims. There was previously two particular cases where this would not happen.
  • Loading branch information
TomNicholas committed Sep 26, 2019
1 parent 85c9a40 commit ea101f5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
8 changes: 6 additions & 2 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/crusaderky>`_.
- 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 <http://github.com/TomNicholas>`_.

Documentation
~~~~~~~~~~~~~
Expand Down Expand Up @@ -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 <https://github.com/spencerkclark>`_.
- 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 <http://github.com/TomNicholas>`_.
- 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 <http://github.com/TomNicholas>`_.
- 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
Expand Down
8 changes: 6 additions & 2 deletions xarray/plot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]
Expand Down
17 changes: 17 additions & 0 deletions xarray/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit ea101f5

Please sign in to comment.