Skip to content

Commit c771eae

Browse files
committed
Squashed plot-transpose
commit d430ae0 Author: dcherian <[email protected]> Date: Wed Nov 13 08:27:04 2019 -0700 proper fix. commit 7fd69be Author: dcherian <[email protected]> Date: Wed Nov 13 08:03:26 2019 -0700 fix whats-new merge. commit 4489394 Merge: 279ff1d b74f80c Author: dcherian <[email protected]> Date: Wed Nov 13 08:03:06 2019 -0700 Merge remote-tracking branch 'upstream/master' into fix/plot-broadcast * upstream/master: format indexing.rst code with black (pydata#3511) add missing pint integration tests (pydata#3508) DOC: update bottleneck repo url (pydata#3507) add drop_sel, drop_vars, map to api.rst (pydata#3506) remove syntax warning (pydata#3505) Dataset.map, GroupBy.map, Resample.map (pydata#3459) tests for datasets with units (pydata#3447) fix pandas-dev tests (pydata#3491) unpin pseudonetcdf (pydata#3496) whatsnew corrections (pydata#3494) drop_vars; deprecate drop for variables (pydata#3475) uamiv test using only raw uamiv variables (pydata#3485) Optimize dask array equality checks. (pydata#3453) Propagate indexes in DataArray binary operations. (pydata#3481) python 3.8 tests (pydata#3477) commit 279ff1d Author: dcherian <[email protected]> Date: Wed Nov 13 08:02:44 2019 -0700 Undo the transpose change and add test to make sure transposition is right. commit c9cc698 Author: dcherian <[email protected]> Date: Wed Nov 13 08:01:39 2019 -0700 Test to make sure transpose is right commit 9b35ecf Author: dcherian <[email protected]> Date: Sat Nov 2 15:49:08 2019 -0600 Additional test. commit 7aed950 Author: dcherian <[email protected]> Date: Sat Nov 2 15:20:07 2019 -0600 make plotting work with transposed nondim coords.
1 parent ecc928c commit c771eae

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ Bug fixes
9999
(:issue:`3402`). By `Deepak Cherian <https://github.com/dcherian/>`_
100100
- Allow appending datetime and bool data variables to zarr stores.
101101
(:issue:`3480`). By `Akihiro Matsukawa <https://github.com/amatsukawa/>`_.
102+
- Fix plotting with transposed 2D non-dimensional coordinates. (:issue:`3138`)
103+
By `Deepak Cherian <https://github.com/dcherian>`_.
102104

103105
Documentation
104106
~~~~~~~~~~~~~

xarray/plot/plot.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -698,10 +698,22 @@ def newplotfunc(
698698

699699
# check if we need to broadcast one dimension
700700
if xval.ndim < yval.ndim:
701-
xval = np.broadcast_to(xval, yval.shape)
701+
dims = darray[ylab].dims
702+
if xval.shape[0] == yval.shape[0]:
703+
xval = np.broadcast_to(xval[:, np.newaxis], yval.shape)
704+
else:
705+
xval = np.broadcast_to(xval[np.newaxis, :], yval.shape)
702706

703-
if yval.ndim < xval.ndim:
704-
yval = np.broadcast_to(yval, xval.shape)
707+
elif yval.ndim < xval.ndim:
708+
dims = darray[xlab].dims
709+
if yval.shape[0] == xval.shape[0]:
710+
yval = np.broadcast_to(yval[:, np.newaxis], xval.shape)
711+
else:
712+
yval = np.broadcast_to(yval[np.newaxis, :], xval.shape)
713+
elif xval.ndim == 2:
714+
dims = darray[xlab].dims
715+
else:
716+
dims = (darray[ylab].dims[0], darray[xlab].dims[0])
705717

706718
# May need to transpose for correct x, y labels
707719
# xlab may be the name of a coord, we have to check for dim names
@@ -711,10 +723,9 @@ def newplotfunc(
711723
# we transpose to (y, x, color) to make this work.
712724
yx_dims = (ylab, xlab)
713725
dims = yx_dims + tuple(d for d in darray.dims if d not in yx_dims)
714-
if dims != darray.dims:
715-
darray = darray.transpose(*dims, transpose_coords=True)
716-
elif darray[xlab].dims[-1] == darray.dims[0]:
717-
darray = darray.transpose(transpose_coords=True)
726+
727+
if dims != darray.dims:
728+
darray = darray.transpose(*dims, transpose_coords=True)
718729

719730
# Pass the data as a masked ndarray too
720731
zval = darray.to_masked_array(copy=False)

xarray/tests/test_plot.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ def test2d_1d_2d_coordinates_contourf(self):
270270
)
271271

272272
a.plot.contourf(x="time", y="depth")
273+
a.plot.contourf(x="depth", y="time")
273274

274275
def test3d(self):
275276
self.darray.plot()
@@ -2156,6 +2157,34 @@ def test_yticks_kwarg(self, da):
21562157
assert np.all(plt.gca().get_yticks() == expected)
21572158

21582159

2160+
@requires_matplotlib
2161+
@pytest.mark.parametrize("plotfunc", ["pcolormesh", "contourf", "contour"])
2162+
def test_plot_transposed_nondim_coord(plotfunc):
2163+
x = np.linspace(0, 10, 101)
2164+
h = np.linspace(3, 7, 101)
2165+
s = np.linspace(0, 1, 51)
2166+
z = s[:, np.newaxis] * h[np.newaxis, :]
2167+
da = xr.DataArray(
2168+
np.sin(x) * np.cos(z),
2169+
dims=["s", "x"],
2170+
coords={"x": x, "s": s, "z": (("s", "x"), z), "zt": (("x", "s"), z.T)},
2171+
)
2172+
getattr(da.plot, plotfunc)(x="x", y="zt")
2173+
getattr(da.plot, plotfunc)(x="zt", y="x")
2174+
2175+
2176+
@requires_matplotlib
2177+
@pytest.mark.parametrize("plotfunc", ["pcolormesh", "imshow"])
2178+
def test_plot_transposes_properly(plotfunc):
2179+
# test that we aren't mistakenly transposing when the 2 dimensions have equal sizes.
2180+
da = xr.DataArray([np.sin(2 * np.pi / 10 * np.arange(10))] * 10, dims=("y", "x"))
2181+
hdl = getattr(da.plot, plotfunc)(x="x", y="y")
2182+
# get_array doesn't work for contour, contourf. It returns the colormap intervals.
2183+
# pcolormesh returns 1D array but imshow returns a 2D array so it is necessary
2184+
# to ravel() on the LHS
2185+
assert np.all(hdl.get_array().ravel() == da.to_masked_array().ravel())
2186+
2187+
21592188
@requires_matplotlib
21602189
class TestDataArrayGroupByPlot:
21612190
@requires_cftime

0 commit comments

Comments
 (0)