From d5f51ef0003fa279877229fe5041e00cac011f09 Mon Sep 17 00:00:00 2001 From: Zac-HD Date: Thu, 14 Dec 2017 23:36:23 +1100 Subject: [PATCH] Handle array dim with size one in plt.imshow --- xarray/plot/plot.py | 13 +++++++++++-- xarray/tests/test_plot.py | 4 ++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index 56a8f643cdb..94556d70f6c 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -580,8 +580,15 @@ def imshow(x, y, z, ax, **kwargs): 'pcolormesh or contour(f)') # Centering the pixels- Assumes uniform spacing - xstep = (x[1] - x[0]) / 2.0 - ystep = (y[1] - y[0]) / 2.0 + try: + xstep = (x[1] - x[0]) / 2.0 + except IndexError: + # Arbitrary default value, similar to matplotlib behaviour + xstep = .1 + try: + ystep = (y[1] - y[0]) / 2.0 + except IndexError: + ystep = .1 left, right = x[0] - xstep, x[-1] + xstep bottom, top = y[-1] + ystep, y[0] - ystep @@ -634,6 +641,8 @@ def _infer_interval_breaks(coord, axis=0): """ coord = np.asarray(coord) deltas = 0.5 * np.diff(coord, axis=axis) + if deltas.size == 0: + deltas = np.array(0.0) first = np.take(coord, [0], axis=axis) - np.take(deltas, [0], axis=axis) last = np.take(coord, [-1], axis=axis) + np.take(deltas, [-1], axis=axis) trim_last = tuple(slice(None, -1) if n == axis else slice(None) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 8e0d306c2cf..1d62f7856f9 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -628,6 +628,10 @@ def test_can_plot_all_nans(self): # regression test for issue #1780 self.plotfunc(DataArray(np.full((2, 2), np.nan))) + def test_can_plot_axis_size_one(self): + if self.plotfunc.__name__ not in ('contour', 'contourf'): + self.plotfunc(DataArray(np.ones((1, 1)))) + def test_viridis_cmap(self): cmap_name = self.plotmethod(cmap='viridis').get_cmap().name self.assertEqual('viridis', cmap_name)