Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit aa66aee

Browse filesBrowse 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 1f95423 commit aa66aee
Copy full SHA for aa66aee

File tree

Expand file treeCollapse file tree

2 files changed

+23
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+23
-2
lines changed

‎lib/matplotlib/image.py

Copy file name to clipboardExpand all lines: lib/matplotlib/image.py
+13-2Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,19 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
367367
scaled_dtype = np.float32
368368
# old versions of numpy do not work with `np.nammin`
369369
# and `np.nanmax` as inputs
370-
a_min = np.ma.min(A).astype(scaled_dtype)
371-
a_max = np.ma.max(A).astype(scaled_dtype)
370+
a_min = np.ma.min(A)
371+
a_max = np.ma.max(A)
372+
373+
# we need these try/except blocks to handle
374+
# fully-masked/invalid input
375+
try:
376+
a_min = a_min.astype(scaled_dtype)
377+
except AttributeError:
378+
a_min = 0
379+
try:
380+
a_max = a_max.astype(scaled_dtype)
381+
except AttributeError:
382+
a_min = 1
372383
# scale the input data to [.1, .9]. The Agg
373384
# interpolators clip to [0, 1] internally, use a
374385
# smaller input scale to identify which of the

‎lib/matplotlib/tests/test_image.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_image.py
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,3 +825,13 @@ def test_imshow_float128():
825825
def test_imshow_bool():
826826
fig, ax = plt.subplots()
827827
ax.imshow(np.array([[True, False], [False, True]], dtype=bool))
828+
829+
830+
def test_full_invalid():
831+
x = np.ones((10, 10))
832+
x[:] = np.nan
833+
834+
f, ax = plt.subplots()
835+
ax.imshow(x)
836+
837+
f.canvas.draw()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.