Skip to content

Commit 7aed950

Browse files
committed
make plotting work with transposed nondim coords.
1 parent 53c5199 commit 7aed950

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ Bug fixes
7676
:py:meth:`xarray.core.groupby.DatasetGroupBy.reduce` when reducing over multiple dimensions.
7777
(:issue:`3402`). By `Deepak Cherian <https://github.com/dcherian/>`_
7878

79+
- Fix plotting with transposed 2D non-dimensional coordinates. (:issue:`3138`)
80+
By `Deepak Cherian <https://github.com/dcherian>`_.
81+
7982
Documentation
8083
~~~~~~~~~~~~~
8184
- Fix leap year condition in example (http://xarray.pydata.org/en/stable/examples/monthly-means.html) by `Mickaël Lalande <https://github.com/mickaellalande>`_.

xarray/plot/plot.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -672,10 +672,16 @@ def newplotfunc(
672672

673673
# check if we need to broadcast one dimension
674674
if xval.ndim < yval.ndim:
675-
xval = np.broadcast_to(xval, yval.shape)
675+
if xval.shape[0] == yval.shape[0]:
676+
xval = np.broadcast_to(xval[:, np.newaxis], yval.shape)
677+
else:
678+
xval = np.broadcast_to(xval[np.newaxis, :], yval.shape)
676679

677-
if yval.ndim < xval.ndim:
678-
yval = np.broadcast_to(yval, xval.shape)
680+
elif yval.ndim < xval.ndim:
681+
if yval.shape[0] == xval.shape[0]:
682+
yval = np.broadcast_to(yval[:, np.newaxis], xval.shape)
683+
else:
684+
yval = np.broadcast_to(yval[np.newaxis, :], xval.shape)
679685

680686
# May need to transpose for correct x, y labels
681687
# xlab may be the name of a coord, we have to check for dim names
@@ -687,7 +693,7 @@ def newplotfunc(
687693
dims = yx_dims + tuple(d for d in darray.dims if d not in yx_dims)
688694
if dims != darray.dims:
689695
darray = darray.transpose(*dims, transpose_coords=True)
690-
elif darray[xlab].dims[-1] == darray.dims[0]:
696+
elif xval.shape[-1] == darray.shape[0]:
691697
darray = darray.transpose(transpose_coords=True)
692698

693699
# Pass the data as a masked ndarray too

xarray/tests/test_plot.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,3 +2145,19 @@ def test_yticks_kwarg(self, da):
21452145
da.plot(yticks=np.arange(5))
21462146
expected = np.arange(5)
21472147
assert np.all(plt.gca().get_yticks() == expected)
2148+
2149+
2150+
@requires_matplotlib
2151+
@pytest.mark.parametrize("plotfunc", ["pcolormesh", "contourf", "contour"])
2152+
def test_plot_transposed_nondim_coord(plotfunc):
2153+
x = np.linspace(0, 10, 101)
2154+
h = np.linspace(3, 7, 101)
2155+
s = np.linspace(0, 1, 51)
2156+
z = s[:, np.newaxis] * h[np.newaxis, :]
2157+
da = xr.DataArray(
2158+
np.sin(x) * np.cos(z),
2159+
dims=["s", "x"],
2160+
coords={"x": x, "s": s, "z": (("s", "x"), z), "zt": (("x", "s"), z.T)},
2161+
)
2162+
getattr(da.plot, plotfunc)(x="x", y="zt")
2163+
getattr(da.plot, plotfunc)(x="zt", y="x")

0 commit comments

Comments
 (0)