Skip to content

Bugfix/plot accept coord dim #3345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 26, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
@@ -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
~~~~~~~~~~~~~
@@ -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
8 changes: 6 additions & 2 deletions xarray/plot/plot.py
Original file line number Diff line number Diff line change
@@ -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]
17 changes: 17 additions & 0 deletions xarray/tests/test_plot.py
Original file line number Diff line number Diff line change
@@ -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()