From 8b08ad853b187aba9cd501704d065f9db7876e51 Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Wed, 29 Nov 2017 03:50:09 -0800 Subject: [PATCH] WIP: 1d+2d coord plotting (#1737) * If one coord for contour plot is 2d, broadcast! * Some kind of test... --- doc/whats-new.rst | 4 ++++ xarray/plot/plot.py | 7 +++++++ xarray/tests/test_plot.py | 9 +++++++++ 3 files changed, 20 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 472cd47adf1..899175af45f 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -21,6 +21,10 @@ v0.10.1 (unreleased) Enhancements ~~~~~~~~~~~~ +- :py:func:`~plot.contourf()` learned to contour 2D variables that have both a 1D co-ordinate (e.g. time) and a 2D co-ordinate (e.g. depth as a function of time). + By `Deepak Cherian `_. + + Bug fixes ~~~~~~~~~ diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index a908de65362..4dd1b1284cd 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -453,6 +453,13 @@ def newplotfunc(darray, x=None, y=None, figsize=None, size=None, yval = darray[ylab].values zval = darray.to_masked_array(copy=False) + # check if we need to broadcast one dimension + if xval.ndim < yval.ndim: + xval = np.broadcast_to(xval, yval.shape) + + if yval.ndim < xval.ndim: + yval = np.broadcast_to(yval, xval.shape) + # May need to transpose for correct x, y labels # xlab may be the name of a coord, we have to check for dim names if darray[xlab].dims[-1] == darray.dims[0]: diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index e2473d6389e..ea0ffa6f3a3 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -107,6 +107,15 @@ def test2d_nonuniform_calls_contourf(self): a.coords['dim_1'] = [2, 1, 89] self.assertTrue(self.contourf_called(a.plot.contourf)) + def test2d_1d_2d_coordinates_contourf(self): + sz = (20, 10) + depth = easy_array(sz) + a = DataArray(easy_array(sz), dims=['z', 'time'], + coords={'depth': (['z', 'time'], depth), + 'time': np.linspace(0, 1, sz[1])}) + + a.plot.contourf(x='time', y='depth') + def test3d(self): self.darray.plot()