Skip to content

Commit

Permalink
Handle array dim with size one in plt.imshow
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Dec 15, 2017
1 parent cb45df0 commit d5f51ef
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 11 additions & 2 deletions xarray/plot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions xarray/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d5f51ef

Please sign in to comment.