Skip to content

Commit ea101f5

Browse files
authored
Bugfix/plot accept coord dim (#3345)
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.
1 parent 85c9a40 commit ea101f5

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

doc/whats-new.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Bug fixes
2424
reinstated for :class:`DataArray` and :class:`Dataset` objects only. Internal xarray
2525
objects remain unaddressable by weakref in order to save memory.
2626
(:issue:`3317`) by `Guido Imperiale <https://github.com/crusaderky>`_.
27+
- Line plots with the ``x`` or ``y`` argument set to a 1D non-dimensional coord
28+
now plot the correct data for 2D DataArrays.
29+
(:issue:`3334`). By `Tom Nicholas <http://github.com/TomNicholas>`_.
2730

2831
Documentation
2932
~~~~~~~~~~~~~
@@ -544,8 +547,9 @@ Bug fixes
544547
from higher frequencies to lower frequencies. Datapoints outside the bounds
545548
of the original time coordinate are now filled with NaN (:issue:`2197`). By
546549
`Spencer Clark <https://github.com/spencerkclark>`_.
547-
- Line plots with the ``x`` argument set to a non-dimensional coord now plot the correct data for 1D DataArrays.
548-
(:issue:`27251`). By `Tom Nicholas <http://github.com/TomNicholas>`_.
550+
- Line plots with the ``x`` argument set to a non-dimensional coord now plot
551+
the correct data for 1D DataArrays.
552+
(:issue:`2725`). By `Tom Nicholas <http://github.com/TomNicholas>`_.
549553
- Subtracting a scalar ``cftime.datetime`` object from a
550554
:py:class:`CFTimeIndex` now results in a :py:class:`pandas.TimedeltaIndex`
551555
instead of raising a ``TypeError`` (:issue:`2671`). By `Spencer Clark

xarray/plot/plot.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ def _infer_line_data(darray, x, y, hue):
8383
)
8484

8585
else:
86-
yplt = darray.transpose(xname, huename)
86+
xdim, = darray[xname].dims
87+
huedim, = darray[huename].dims
88+
yplt = darray.transpose(xdim, huedim)
8789

8890
else:
8991
yname, huename = _infer_xy_labels(darray=darray, x=y, y=hue)
@@ -100,7 +102,9 @@ def _infer_line_data(darray, x, y, hue):
100102
)
101103

102104
else:
103-
xplt = darray.transpose(yname, huename)
105+
ydim, = darray[yname].dims
106+
huedim, = darray[huename].dims
107+
xplt = darray.transpose(ydim, huedim)
104108

105109
huelabel = label_from_attrs(darray[huename])
106110
hueplt = darray[huename]

xarray/tests/test_plot.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,23 @@ def test_infer_line_data(self):
170170
line = current.plot.line()[0]
171171
assert_array_equal(line.get_xdata(), current.coords["t"].values)
172172

173+
def test_line_plot_along_1d_coord(self):
174+
# Test for bug in GH #3334
175+
x_coord = xr.DataArray(data=[0.1, 0.2], dims=["x"])
176+
t_coord = xr.DataArray(data=[10, 20], dims=["t"])
177+
178+
da = xr.DataArray(
179+
data=np.array([[0, 1], [5, 9]]),
180+
dims=["x", "t"],
181+
coords={"x": x_coord, "time": t_coord},
182+
)
183+
184+
line = da.plot(x="time", hue="x")[0]
185+
assert_array_equal(line.get_xdata(), da.coords["time"].values)
186+
187+
line = da.plot(y="time", hue="x")[0]
188+
assert_array_equal(line.get_ydata(), da.coords["time"].values)
189+
173190
def test_2d_line(self):
174191
with raises_regex(ValueError, "hue"):
175192
self.darray[:, :, 0].plot.line()

0 commit comments

Comments
 (0)