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

Skip to content

Commit 32e727b

Browse files
committed
bugs in colors.BoundaryNorm
1 parent 7539b61 commit 32e727b

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

lib/matplotlib/colors.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,22 +1270,22 @@ def __init__(self, boundaries, ncolors, clip=False):
12701270
def __call__(self, x, clip=None):
12711271
if clip is None:
12721272
clip = self.clip
1273-
x = ma.asarray(x)
1274-
mask = ma.getmaskarray(x)
1275-
xx = x.filled(self.vmax + 1)
1273+
_x = ma.asarray(x)
1274+
mask = ma.getmaskarray(_x)
1275+
xx = np.atleast_1d(_x.filled(self.vmax + 1))
12761276
if clip:
1277-
np.clip(xx, self.vmin, self.vmax)
1278-
iret = np.zeros(x.shape, dtype=np.int16)
1277+
np.clip(xx, self.vmin, self.vmax, out=xx)
1278+
iret = np.atleast_1d(np.zeros(_x.shape, dtype=np.int16))
12791279
for i, b in enumerate(self.boundaries):
12801280
iret[xx >= b] = i
12811281
if self._interp:
12821282
scalefac = float(self.Ncmap - 1) / (self.N - 2)
12831283
iret = (iret * scalefac).astype(np.int16)
12841284
iret[xx < self.vmin] = -1
1285-
iret[xx >= self.vmax] = self.Ncmap
1285+
iret[xx >= self.vmax] = self.Ncmap # ISSUE here: clip is ignored
12861286
ret = ma.array(iret, mask=mask)
1287-
if ret.shape == () and not mask:
1288-
ret = int(ret) # assume python scalar
1287+
if _x.shape == () and not mask:
1288+
ret = int(ret[0]) # assume python scalar
12891289
return ret
12901290

12911291
def inverse(self, value):

lib/matplotlib/tests/test_colors.py

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import itertools
66
from distutils.version import LooseVersion as V
77

8-
from nose.tools import assert_raises, assert_equal
8+
from nose.tools import assert_raises, assert_equal, assert_true
99

1010
import numpy as np
1111
from numpy.testing.utils import assert_array_equal, assert_array_almost_equal
@@ -39,15 +39,60 @@ def test_BoundaryNorm():
3939
Github issue #1258: interpolation was failing with numpy
4040
1.7 pre-release.
4141
"""
42-
# TODO: expand this into a more general test of BoundaryNorm.
42+
4343
boundaries = [0, 1.1, 2.2]
44-
vals = [-1, 0, 2, 2.2, 4]
45-
expected = [-1, 0, 2, 3, 3]
44+
vals = [-1, 0, 1, 2, 2.2, 4]
45+
46+
# Without interpolation
47+
expected = [-1, 0, 0, 1, 2, 2]
48+
ncolors = len(boundaries) - 1
49+
bn = mcolors.BoundaryNorm(boundaries, ncolors)
50+
assert_array_equal(bn(vals), expected)
51+
4652
# ncolors != len(boundaries) - 1 triggers interpolation
53+
expected = [-1, 0, 0, 2, 3, 3]
4754
ncolors = len(boundaries)
4855
bn = mcolors.BoundaryNorm(boundaries, ncolors)
4956
assert_array_equal(bn(vals), expected)
5057

58+
# more boundaries for a third color
59+
boundaries = [0, 1, 2, 3]
60+
vals = [-1, 0.1, 1.1, 2.2, 4]
61+
ncolors = 5
62+
expected = [-1, 0, 2, 4, 5]
63+
bn = mcolors.BoundaryNorm(boundaries, ncolors)
64+
assert_array_equal(bn(vals), expected)
65+
66+
# a scalar as input should not trigger an error and should return a scalar
67+
boundaries = [0, 1, 2]
68+
vals = [-1, 0.1, 1.1, 2.2]
69+
bn = mcolors.BoundaryNorm(boundaries, 2)
70+
expected = [-1, 0, 1, 2]
71+
for v, ex in zip(vals, expected):
72+
ret = bn(v)
73+
assert_true(isinstance(ret, six.integer_types))
74+
assert_array_equal(ret, ex)
75+
assert_array_equal(bn([v]), ex)
76+
77+
# same with interp
78+
bn = mcolors.BoundaryNorm(boundaries, 3)
79+
expected = [-1, 0, 2, 3]
80+
for v, ex in zip(vals, expected):
81+
ret = bn(v)
82+
assert_true(isinstance(ret, six.integer_types))
83+
assert_array_equal(ret, ex)
84+
assert_array_equal(bn([v]), ex)
85+
86+
# Clipping
87+
# For the "3" I do not know: isn't clipping also supposed to clip the max?
88+
bn = mcolors.BoundaryNorm(boundaries, 3, clip=True)
89+
expected = [0, 0, 2, 3]
90+
for v, ex in zip(vals, expected):
91+
ret = bn(v)
92+
assert_true(isinstance(ret, six.integer_types))
93+
assert_array_equal(ret, ex)
94+
assert_array_equal(bn([v]), ex)
95+
5196

5297
def test_LogNorm():
5398
"""

0 commit comments

Comments
 (0)