Skip to content

Commit

Permalink
WIP: 1d+2d coord plotting (#1737)
Browse files Browse the repository at this point in the history
* If one coord for contour plot is 2d, broadcast!

* Some kind of test...
  • Loading branch information
dcherian authored and shoyer committed Nov 29, 2017
1 parent 9d09c16 commit 8b08ad8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/dcherian>`_.


Bug fixes
~~~~~~~~~

Expand Down
7 changes: 7 additions & 0 deletions xarray/plot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
9 changes: 9 additions & 0 deletions xarray/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 8b08ad8

Please sign in to comment.