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

Skip to content

Commit ab3e42f

Browse files
committed
Merge pull request matplotlib#1260 from efiring/boundary_norm_interp
Fix BoundaryNorm interpolation with numpy 1.7rc.
2 parents 0a8a110 + 1a0f05a commit ab3e42f

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/matplotlib/colors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,8 +1056,8 @@ def __call__(self, x, clip=None):
10561056
for i, b in enumerate(self.boundaries):
10571057
iret[xx >= b] = i
10581058
if self._interp:
1059-
iret *= float(self.Ncmap - 1) / (self.N - 2)
1060-
iret = iret.astype(np.int16)
1059+
scalefac = float(self.Ncmap - 1) / (self.N - 2)
1060+
iret = (iret * scalefac).astype(np.int16)
10611061
iret[xx < self.vmin] = -1
10621062
iret[xx >= self.vmax] = self.Ncmap
10631063
ret = ma.array(iret, mask=mask)

lib/matplotlib/tests/test_colors.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,17 @@ def test_colormap_endian():
2323
#print(anative.dtype.isnative, aforeign.dtype.isnative)
2424
assert_array_equal(cmap(anative), cmap(aforeign))
2525

26+
def test_BoundaryNorm():
27+
"""
28+
Github issue #1258: interpolation was failing with numpy
29+
1.7 pre-release.
30+
"""
31+
# TODO: expand this into a more general test of BoundaryNorm.
32+
boundaries = [0, 1.1, 2.2]
33+
vals = [-1, 0, 2, 2.2, 4]
34+
expected = [-1, 0, 2, 3, 3]
35+
# ncolors != len(boundaries) - 1 triggers interpolation
36+
ncolors = len(boundaries)
37+
bn = mcolors.BoundaryNorm(boundaries, ncolors)
38+
assert_array_equal(bn(vals), expected)
2639

0 commit comments

Comments
 (0)