diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 7310b984a2c..2b13d61a119 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -22,6 +22,10 @@ v0.9.2 (unreleased) Enhancements ~~~~~~~~~~~~ +- When `plot()` is called on a 2D DataArray and only one dimension is + specified with `x=` or `y=`, the other dimension is now guessed. By + `Vincent Noel `_. + Bug fixes ~~~~~~~~~ - ``rolling`` now keeps its original dimension order (:issue:`1125`). diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index db926c06fa1..adc71d860b9 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -221,8 +221,14 @@ def _infer_xy_labels(darray, x, y): if darray.ndim != 2: raise ValueError('DataArray must be 2d') y, x = darray.dims - elif x is None or y is None: - raise ValueError('cannot supply only one of x and y') + elif x is None: + if y not in darray.dims: + raise ValueError('y must be a dimension name if x is not supplied') + x = darray.dims[0] if y == darray.dims[1] else darray.dims[1] + elif y is None: + if x not in darray.dims: + raise ValueError('x must be a dimension name if y is not supplied') + y = darray.dims[0] if x == darray.dims[1] else darray.dims[1] elif any(k not in darray.coords and k not in darray.dims for k in (x, y)): raise ValueError('x and y must be coordinate variables') return x, y diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 465f0ec5f31..0d92bf86294 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -636,17 +636,27 @@ def test_xy_strings(self): self.assertEqual('x', ax.get_ylabel()) def test_positional_coord_string(self): - with self.assertRaisesRegexp(ValueError, 'cannot supply only one'): - self.plotmethod('y') - with self.assertRaisesRegexp(ValueError, 'cannot supply only one'): - self.plotmethod(y='x') + self.plotmethod(y='x') + ax = plt.gca() + self.assertEqual('x', ax.get_ylabel()) + self.assertEqual('y', ax.get_xlabel()) + + self.plotmethod(x='x') + ax = plt.gca() + self.assertEqual('x', ax.get_xlabel()) + self.assertEqual('y', ax.get_ylabel()) def test_bad_x_string_exception(self): - with self.assertRaisesRegexp(ValueError, 'x and y must be coordinate'): + with self.assertRaisesRegexp( + ValueError, 'x and y must be coordinate variables'): self.plotmethod('not_a_real_dim', 'y') + with self.assertRaisesRegexp( + ValueError, 'x must be a dimension name if y is not supplied'): + self.plotmethod(x='not_a_real_dim') + with self.assertRaisesRegexp( + ValueError, 'y must be a dimension name if x is not supplied'): + self.plotmethod(y='not_a_real_dim') self.darray.coords['z'] = 100 - with self.assertRaisesRegexp(ValueError, 'cannot supply only one'): - self.plotmethod('z') def test_coord_strings(self): # 1d coords (same as dims)