Skip to content

Commit afbdbc7

Browse files
vnoelfmaussion
authored andcommitted
Guess the complementary dimension when only one is passed to pcolormesh (#1291)
* Guess the complementary dimension when only one is passed to pcolormesh * Add tests, verify the requested dimension is in the array * fix typo * updated whatsnew * Forgot to add my name * more accurate error message when specifying only one axis during a call to pcolormesh. Fix related tests * fix typo * make style flake8-compliant
1 parent e92a807 commit afbdbc7

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

doc/whats-new.rst

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ Enhancements
2626
now also accepts a `min_periods` argument (:issue:`1276`).
2727
By `Joe Hamman <https://github.com/jhamman>`_.
2828

29+
- When `plot()` is called on a 2D DataArray and only one dimension is
30+
specified with `x=` or `y=`, the other dimension is now guessed. By
31+
`Vincent Noel <https://github.com/vnoel>`_.
32+
2933
Bug fixes
3034
~~~~~~~~~
3135
- ``rolling`` now keeps its original dimension order (:issue:`1125`).

xarray/plot/utils.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,14 @@ def _infer_xy_labels(darray, x, y):
221221
if darray.ndim != 2:
222222
raise ValueError('DataArray must be 2d')
223223
y, x = darray.dims
224-
elif x is None or y is None:
225-
raise ValueError('cannot supply only one of x and y')
224+
elif x is None:
225+
if y not in darray.dims:
226+
raise ValueError('y must be a dimension name if x is not supplied')
227+
x = darray.dims[0] if y == darray.dims[1] else darray.dims[1]
228+
elif y is None:
229+
if x not in darray.dims:
230+
raise ValueError('x must be a dimension name if y is not supplied')
231+
y = darray.dims[0] if x == darray.dims[1] else darray.dims[1]
226232
elif any(k not in darray.coords and k not in darray.dims for k in (x, y)):
227233
raise ValueError('x and y must be coordinate variables')
228234
return x, y

xarray/tests/test_plot.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -636,17 +636,27 @@ def test_xy_strings(self):
636636
self.assertEqual('x', ax.get_ylabel())
637637

638638
def test_positional_coord_string(self):
639-
with self.assertRaisesRegexp(ValueError, 'cannot supply only one'):
640-
self.plotmethod('y')
641-
with self.assertRaisesRegexp(ValueError, 'cannot supply only one'):
642-
self.plotmethod(y='x')
639+
self.plotmethod(y='x')
640+
ax = plt.gca()
641+
self.assertEqual('x', ax.get_ylabel())
642+
self.assertEqual('y', ax.get_xlabel())
643+
644+
self.plotmethod(x='x')
645+
ax = plt.gca()
646+
self.assertEqual('x', ax.get_xlabel())
647+
self.assertEqual('y', ax.get_ylabel())
643648

644649
def test_bad_x_string_exception(self):
645-
with self.assertRaisesRegexp(ValueError, 'x and y must be coordinate'):
650+
with self.assertRaisesRegexp(
651+
ValueError, 'x and y must be coordinate variables'):
646652
self.plotmethod('not_a_real_dim', 'y')
653+
with self.assertRaisesRegexp(
654+
ValueError, 'x must be a dimension name if y is not supplied'):
655+
self.plotmethod(x='not_a_real_dim')
656+
with self.assertRaisesRegexp(
657+
ValueError, 'y must be a dimension name if x is not supplied'):
658+
self.plotmethod(y='not_a_real_dim')
647659
self.darray.coords['z'] = 100
648-
with self.assertRaisesRegexp(ValueError, 'cannot supply only one'):
649-
self.plotmethod('z')
650660

651661
def test_coord_strings(self):
652662
# 1d coords (same as dims)

0 commit comments

Comments
 (0)