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

Skip to content

Commit a5f5317

Browse files
committed
ENH: pick the middle color only 1 region with BoundaryNorm
This never worked before so is not an API change.
1 parent 64fff76 commit a5f5317

2 files changed

Lines changed: 7 additions & 9 deletions

File tree

lib/matplotlib/colors.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,17 +1511,15 @@ def __call__(self, value, clip=None):
15111511
# in the middle) such that the first region is mapped to the
15121512
# first color and the last region is mapped to the last color.
15131513
if self.Ncmap > self._n_regions:
1514-
# the maximum possible value in iret
1515-
divsor = self._n_regions - 1
1516-
if divsor == 0:
1517-
# special case the 1 region case, don't scale anything
1518-
scalefac = 1
1514+
if self._n_regions == 1:
1515+
# special case the 1 region case, pick the middle color
1516+
iret[iret == 0] = (self.Ncmap - 1) // 2
15191517
else:
15201518
# otherwise linearly remap the values from the region index
15211519
# to the color index spaces
1522-
scalefac = (self.Ncmap - 1) / divsor
1523-
# do the scaling and re-cast to integers
1524-
iret = (iret * scalefac).astype(np.int16)
1520+
iret = (self.Ncmap - 1) / (self._n_regions - 1) * iret
1521+
# cast to 16bit integers in all cases
1522+
iret = iret.astype(np.int16)
15251523
iret[xx < self.vmin] = -1
15261524
iret[xx >= self.vmax] = max_col
15271525
ret = np.ma.array(iret, mask=mask)

lib/matplotlib/tests/test_colors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def test_BoundaryNorm():
208208
assert_array_equal(bn(vals), expected)
209209

210210
# with a single region and interpolation
211-
expected = [-1, 0, 0, 0, 3, 3]
211+
expected = [-1, 1, 1, 1, 3, 3]
212212
bn = mcolors.BoundaryNorm([0, 2.2], ncolors)
213213
assert_array_equal(bn(vals), expected)
214214

0 commit comments

Comments
 (0)