Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7fab129

Browse files
committed
added tests for masked array and clipping
1 parent 32e727b commit 7fab129

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

lib/matplotlib/colors.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,8 +1252,9 @@ def __init__(self, boundaries, ncolors, clip=False):
12521252
as i varies from 0 to len(boundaries)-2,
12531253
j goes from 0 to ncolors-1.
12541254
1255-
Out-of-range values are mapped to -1 if low and ncolors
1256-
if high; these are converted to valid indices by
1255+
If clip == False, Out-of-range values are mapped
1256+
to -1 if low and ncolors if high; these are converted
1257+
to valid indices by
12571258
:meth:`Colormap.__call__` .
12581259
"""
12591260
self.clip = clip
@@ -1270,21 +1271,24 @@ def __init__(self, boundaries, ncolors, clip=False):
12701271
def __call__(self, x, clip=None):
12711272
if clip is None:
12721273
clip = self.clip
1273-
_x = ma.asarray(x)
1274-
mask = ma.getmaskarray(_x)
1275-
xx = np.atleast_1d(_x.filled(self.vmax + 1))
1274+
x = ma.masked_invalid(x, copy=False)
1275+
mask = ma.getmaskarray(x)
1276+
xx = np.atleast_1d(x.filled(self.vmax + 1))
12761277
if clip:
12771278
np.clip(xx, self.vmin, self.vmax, out=xx)
1278-
iret = np.atleast_1d(np.zeros(_x.shape, dtype=np.int16))
1279+
max_col = self.Ncmap - 1
1280+
else:
1281+
max_col = self.Ncmap
1282+
iret = np.atleast_1d(np.zeros(x.shape, dtype=np.int16))
12791283
for i, b in enumerate(self.boundaries):
12801284
iret[xx >= b] = i
12811285
if self._interp:
12821286
scalefac = float(self.Ncmap - 1) / (self.N - 2)
12831287
iret = (iret * scalefac).astype(np.int16)
12841288
iret[xx < self.vmin] = -1
1285-
iret[xx >= self.vmax] = self.Ncmap # ISSUE here: clip is ignored
1289+
iret[xx >= self.vmax] = max_col
12861290
ret = ma.array(iret, mask=mask)
1287-
if _x.shape == () and not mask:
1291+
if x.shape == ():
12881292
ret = int(ret[0]) # assume python scalar
12891293
return ret
12901294

lib/matplotlib/tests/test_colors.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,37 @@ def test_BoundaryNorm():
8484
assert_array_equal(bn([v]), ex)
8585

8686
# Clipping
87-
# For the "3" I do not know: isn't clipping also supposed to clip the max?
8887
bn = mcolors.BoundaryNorm(boundaries, 3, clip=True)
89-
expected = [0, 0, 2, 3]
88+
expected = [0, 0, 2, 2]
9089
for v, ex in zip(vals, expected):
9190
ret = bn(v)
9291
assert_true(isinstance(ret, six.integer_types))
9392
assert_array_equal(ret, ex)
9493
assert_array_equal(bn([v]), ex)
9594

95+
# Masked arrays
96+
boundaries = [0, 1.1, 2.2]
97+
vals = [-1., np.NaN, 0, 1.4, 9]
98+
99+
# Without interpolation
100+
ncolors = len(boundaries) - 1
101+
bn = mcolors.BoundaryNorm(boundaries, ncolors)
102+
expected = np.ma.masked_array([-1, -99, 0, 1, 2], mask=[0, 1, 0, 0, 0])
103+
assert_array_equal(bn(vals), expected)
104+
105+
# With interpolation
106+
bn = mcolors.BoundaryNorm(boundaries, len(boundaries))
107+
expected = np.ma.masked_array([-1, -99, 0, 2, 3], mask=[0, 1, 0, 0, 0])
108+
assert_array_equal(bn(vals), expected)
109+
110+
# Non-trivial masked arrays
111+
vals = [np.Inf, np.NaN]
112+
assert_true(np.all(bn(vals).mask))
113+
vals = [np.Inf]
114+
assert_true(np.all(bn(vals).mask))
115+
116+
# Invalid scalar raises an exception
117+
assert_raises(Exception, bn, np.NaN)
96118

97119
def test_LogNorm():
98120
"""

0 commit comments

Comments
 (0)