Skip to content

Guess the complementary dimension when only one is passed to pcolormesh #1291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/vnoel>`_.

Bug fixes
~~~~~~~~~
- ``rolling`` now keeps its original dimension order (:issue:`1125`).
Expand Down
10 changes: 8 additions & 2 deletions xarray/plot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 17 additions & 7 deletions xarray/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down