From 4ec44bf8eb45dd4880a865c563a8f962d0588850 Mon Sep 17 00:00:00 2001 From: Vincent Noel Date: Thu, 2 Mar 2017 14:28:55 +0100 Subject: [PATCH 1/8] Guess the complementary dimension when only one is passed to pcolormesh --- xarray/plot/utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index db926c06fa1..b8f4803ff9b 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -221,8 +221,10 @@ 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: + x = darray.dims[0] if y == darray.dims[1] else darray.dims[1] + elif y is None: + 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 From fb980a04c40d978d873b216cd1c41dae9d379c12 Mon Sep 17 00:00:00 2001 From: Vincent Noel Date: Thu, 2 Mar 2017 16:19:06 +0100 Subject: [PATCH 2/8] Add tests, verify the requested dimension is in the array --- xarray/plot/utils.py | 4 ++++ xarray/tests/test_plot.py | 21 ++++++++++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index b8f4803ff9b..72127d4d8cb 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -222,8 +222,12 @@ def _infer_xy_labels(darray, x, y): raise ValueError('DataArray must be 2d') y, x = darray.dims elif x is None: + if y not in darray.dims: + raise ValueError('y must be a coordinate variable') 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 coordinate variables') 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') diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 465f0ec5f31..dd0b231d6bd 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -636,17 +636,24 @@ 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 coordinate variable'): + self.plotmethod(x='not_a_real_dim') + with self.assertRaisesRegexp(ValueError, 'y must be a coordinate variable'): + 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) From 1de63e32f8a3c2dca0bc582b9317a6ebde8a338b Mon Sep 17 00:00:00 2001 From: Vincent Noel Date: Thu, 2 Mar 2017 16:23:46 +0100 Subject: [PATCH 3/8] fix typo --- xarray/plot/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index 72127d4d8cb..cb56fea0b5f 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -227,7 +227,7 @@ def _infer_xy_labels(darray, x, y): 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 coordinate variables') + raise ValueError('x must be a coordinate variable') 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') From a3e0b892a448ecc6c565ef5618a4ad4538ed202c Mon Sep 17 00:00:00 2001 From: Vincent Noel Date: Sat, 4 Mar 2017 10:14:51 +0100 Subject: [PATCH 4/8] updated whatsnew --- doc/whats-new.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 7310b984a2c..bdabe85dc2c 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -22,6 +22,9 @@ 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. + Bug fixes ~~~~~~~~~ - ``rolling`` now keeps its original dimension order (:issue:`1125`). From ccb6b9c21c8631c87ea0ae301ef758058b38190e Mon Sep 17 00:00:00 2001 From: Vincent Noel Date: Sat, 4 Mar 2017 10:19:54 +0100 Subject: [PATCH 5/8] Forgot to add my name --- doc/whats-new.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index bdabe85dc2c..60c86fedeff 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -23,7 +23,8 @@ 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. + specified with `x=` or `=y`, the other dimension is now guessed. By + `Vincent Noel `_. Bug fixes ~~~~~~~~~ From 43e81feddaf64e88e8753bb45cb303f08450e25c Mon Sep 17 00:00:00 2001 From: Vincent Noel Date: Mon, 6 Mar 2017 16:31:46 +0100 Subject: [PATCH 6/8] more accurate error message when specifying only one axis during a call to pcolormesh. Fix related tests --- xarray/plot/utils.py | 4 ++-- xarray/tests/test_plot.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index cb56fea0b5f..adc71d860b9 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -223,11 +223,11 @@ def _infer_xy_labels(darray, x, y): y, x = darray.dims elif x is None: if y not in darray.dims: - raise ValueError('y must be a coordinate variable') + 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 coordinate variable') + 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') diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index dd0b231d6bd..89dc4f0e3ba 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -649,9 +649,9 @@ def test_positional_coord_string(self): def test_bad_x_string_exception(self): 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 coordinate variable'): + 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 coordinate variable'): + 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 From 1fd73199f3e7940eadb7bfb9f24c5985beefc3f8 Mon Sep 17 00:00:00 2001 From: Vincent Noel Date: Mon, 6 Mar 2017 16:32:01 +0100 Subject: [PATCH 7/8] fix typo --- doc/whats-new.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 60c86fedeff..2b13d61a119 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -23,7 +23,7 @@ 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 + specified with `x=` or `y=`, the other dimension is now guessed. By `Vincent Noel `_. Bug fixes From 5911be9c62c05d5c8ab3825b501081a0a3b94582 Mon Sep 17 00:00:00 2001 From: Vincent Noel Date: Tue, 7 Mar 2017 13:14:55 +0100 Subject: [PATCH 8/8] make style flake8-compliant --- xarray/tests/test_plot.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 89dc4f0e3ba..0d92bf86294 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -647,11 +647,14 @@ def test_positional_coord_string(self): self.assertEqual('y', ax.get_ylabel()) def test_bad_x_string_exception(self): - with self.assertRaisesRegexp(ValueError, 'x and y must be coordinate variables'): + 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'): + 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'): + 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