Skip to content

Commit 07eaa4c

Browse files
committed
FIX: handle fully masked data
As min/max do not make sense for an array with no non-masked values numpy returns a singleton, `np.ma.masked`, which can not be cast to a number. In this case just treat numbers as in range (0, 1) (even though it will just be ignored due to the masking). closes #9280
1 parent 1976408 commit 07eaa4c

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/matplotlib/image.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,19 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
370370
scaled_dtype = np.float32
371371
# old versions of numpy do not work with `np.nammin`
372372
# and `np.nanmax` as inputs
373-
a_min = np.ma.min(A).astype(scaled_dtype)
374-
a_max = np.ma.max(A).astype(scaled_dtype)
373+
a_min = np.ma.min(A)
374+
a_max = np.ma.max(A)
375+
376+
# we need these try/except blocks to handle
377+
# fully-masked/invalid input
378+
try:
379+
a_min = a_min.astype(scaled_dtype)
380+
except AttributeError:
381+
a_min = 0
382+
try:
383+
a_max = a_max.astype(scaled_dtype)
384+
except AttributeError:
385+
a_min = 1
375386
# scale the input data to [.1, .9]. The Agg
376387
# interpolators clip to [0, 1] internally, use a
377388
# smaller input scale to identify which of the

lib/matplotlib/tests/test_image.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,3 +835,13 @@ def test_imshow_deprecated_interd_warn():
835835
with warnings.catch_warnings(record=True) as warns:
836836
getattr(im, k)
837837
assert len(warns) == 1
838+
839+
840+
def test_full_invalid():
841+
x = np.ones((10, 10))
842+
x[:] = np.nan
843+
844+
f, ax = plt.subplots()
845+
ax.imshow(x)
846+
847+
f.canvas.draw()

0 commit comments

Comments
 (0)