Skip to content

Commit 2d631ca

Browse files
committed
Handle byte-images and avoid mutation
1 parent 9eb4b3a commit 2d631ca

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

xarray/plot/plot.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,9 @@ def newplotfunc(darray, x=None, y=None, figsize=None, size=None,
450450
if imshow_rgb:
451451
# Don't add a colorbar when showing an image with explicit colors
452452
add_colorbar = False
453+
# Convert byte-arrays to float for correct display in matplotlib
454+
if darray.dtype == np.dtype('uint8'):
455+
darray = darray / 256.0
453456
# Manually stretch colors for robust cmap
454457
if robust:
455458
flat = darray.values.ravel(order='K')
@@ -464,8 +467,10 @@ def newplotfunc(darray, x=None, y=None, figsize=None, size=None,
464467
darray = (darray - vmin) / (vmax - vmin)
465468
robust = False
466469
del flat
467-
# Clip range to [0, 1] to avoid visual artefacts
468-
darray.values[:] = np.clip(darray.values, 0, 1)
470+
# Clip range of floats to [0, 1] to avoid visual artefacts
471+
else:
472+
darray = darray.copy(deep=True)
473+
darray.values[:] = np.clip(darray.values, 0, 1)
469474

470475
# Handle facetgrids first
471476
if row or col:

0 commit comments

Comments
 (0)