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

Skip to content

Commit 590f401

Browse files
committed
Fix BoundaryNorm for multiple colors and one region
1 parent 4e6a206 commit 590f401

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

lib/matplotlib/colors.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,9 +1429,9 @@ def __init__(self, boundaries, ncolors, clip=False, *, extend='neither'):
14291429
Parameters
14301430
----------
14311431
boundaries : array-like
1432-
Monotonically increasing sequence of boundaries
1432+
Monotonically increasing sequence of boundaries.
14331433
ncolors : int
1434-
Number of colors in the colormap to be used
1434+
Number of colors in the colormap to be used.
14351435
clip : bool, optional
14361436
If clip is ``True``, out of range values are mapped to 0 if they
14371437
are below ``boundaries[0]`` or mapped to ``ncolors - 1`` if they
@@ -1492,6 +1492,7 @@ def __call__(self, value, clip=None):
14921492

14931493
xx, is_scalar = self.process_value(value)
14941494
mask = np.ma.getmaskarray(xx)
1495+
# Fill masked values a value above the upper boundary
14951496
xx = np.atleast_1d(xx.filled(self.vmax + 1))
14961497
if clip:
14971498
np.clip(xx, self.vmin, self.vmax, out=xx)
@@ -1500,7 +1501,10 @@ def __call__(self, value, clip=None):
15001501
max_col = self.Ncmap
15011502
iret = np.digitize(xx, self.boundaries) - 1 + self._offset
15021503
if self.Ncmap > self._n_regions:
1503-
scalefac = (self.Ncmap - 1) / (self._n_regions - 1)
1504+
if self._n_regions == 1:
1505+
scalefac = 1
1506+
else:
1507+
scalefac = (self.Ncmap - 1) / (self._n_regions - 1)
15041508
iret = (iret * scalefac).astype(np.int16)
15051509
iret[xx < self.vmin] = -1
15061510
iret[xx >= self.vmax] = max_col

lib/matplotlib/tests/test_colors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ def test_BoundaryNorm():
207207
bn = mcolors.BoundaryNorm(boundaries, ncolors)
208208
assert_array_equal(bn(vals), expected)
209209

210+
# with a single region and interpolation
211+
expected = [-1, 0, 0, 0, 3, 3]
212+
bn = mcolors.BoundaryNorm([0, 2.2], ncolors)
213+
assert_array_equal(bn(vals), expected)
214+
210215
# more boundaries for a third color
211216
boundaries = [0, 1, 2, 3]
212217
vals = [-1, 0.1, 1.1, 2.2, 4]

0 commit comments

Comments
 (0)