Skip to content

Commit 8d280cd

Browse files
authored
Bugfix for plotting transposed 2d coords (#3934)
* always transpose y to match x * test based on issue example * updated what's new * isort
1 parent 0181aa5 commit 8d280cd

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ Bug fixes
5959
By `Todd Jennings <https://github.com/toddrjen>`_
6060
- Fix ``FacetGrid`` when ``vmin == vmax``. (:issue:`3734`)
6161
By `Deepak Cherian <https://github.com/dcherian>`_
62+
- Fix bug where plotting line plots with 2D coordinates depended on dimension
63+
order. (:issue:`3933`)
64+
By `Tom Nicholas <https://github.com/TomNicholas>`_.
6265

6366
Documentation
6467
~~~~~~~~~~~~~

xarray/plot/plot.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def _infer_line_data(darray, x, y, hue):
9393
otherindex = 1 if darray.dims.index(huename) == 0 else 0
9494
otherdim = darray.dims[otherindex]
9595
xplt = darray.transpose(otherdim, huename, transpose_coords=False)
96+
yplt = yplt.transpose(otherdim, huename, transpose_coords=False)
9697
else:
9798
raise ValueError(
9899
"For 2D inputs, hue must be a dimension"

xarray/tests/test_plot.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,17 @@ def test_2d_coords_line_plot(self):
257257
with pytest.raises(ValueError, match="For 2D inputs, hue must be a dimension"):
258258
da.plot.line(x="lon", hue="lat")
259259

260+
def test_2d_coord_line_plot_coords_transpose_invariant(self):
261+
# checks for bug reported in GH #3933
262+
x = np.arange(10)
263+
y = np.arange(20)
264+
ds = xr.Dataset(coords={"x": x, "y": y})
265+
266+
for z in [ds.y + ds.x, ds.x + ds.y]:
267+
ds = ds.assign_coords(z=z)
268+
ds["v"] = ds.x + ds.y
269+
ds["v"].plot.line(y="z", hue="x")
270+
260271
def test_2d_before_squeeze(self):
261272
a = DataArray(easy_array((1, 5)))
262273
a.plot()

0 commit comments

Comments
 (0)