8000 added support for extend in colorbarbase · matplotlib/matplotlib@4be7829 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4be7829

Browse files
fmaussionefiring
authored andcommitted
added support for extend in colorbarbase
1 parent 5348428 commit 4be7829

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

lib/matplotlib/colorbar.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,8 @@ def __init__(self, ax, cmap=None,
430430
cmap = cm.get_cmap()
431431
if norm is None:
432432
norm = colors.Normalize()
433+
elif hasattr(norm, 'extend') and norm.extend != 'neither':
434+
extend = norm.extend
433435
self.alpha = alpha
434436
self.cmap = cmap
435437
self.norm = norm

lib/matplotlib/colors.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,9 @@ def __init__(self, boundaries, ncolors, clip=False, extend='neither'):
14391439
If the number of bins doesn't equal *ncolors*, the color is chosen
14401440
by linear interpolation of the bin number onto color numbers.
14411441
"""
1442+
1443+
if clip and extend != 'neither':
1444+
raise ValueError("'clip=True' is not compatible with 'extend'")
14421445
self.clip = clip
14431446
self.vmin = boundaries[0]
14441447
self.vmax = boundaries[-1]
@@ -1447,20 +1450,18 @@ def __init__(self, boundaries, ncolors, clip=False, extend='neither'):
14471450
self.Ncmap = ncolors
14481451

14491452
# Extension. We use the same trick as colorbar.py and add a fake
1450-
# boundary were needed. Since colorbar does it too, we have to use a
1451-
# private property for that.
1453+
# boundary were needed.
14521454
_b = list(boundaries)
14531455
if extend == 'both':
14541456
_b = [_b[0] - 1] + _b + [_b[-1] + 1]
14551457
elif extend == 'min':
14561458
_b = [_b[0] - 1] + _b
14571459
elif extend == 'max':
14581460
_b = _b + [_b[-1] + 1]
1459-
self._b = np.array(_b)
1461+
self.extend = extend
14601462

1461-
# I am not sure if the private _N is necessary (I dont think so,
1462-
# but it should be consistent with boundaries I guess. This _N is
1463-
# used for interpolation).
1463+
# needed for the interpolation but should not be seen from outside
1464+
self._b = np.array(_b)
14641465
self._N = len(self._b)
14651466
if self._N - 1 == self.Ncmap:
14661467
self._interp = False

lib/matplotlib/tests/test_colors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ def test_BoundaryNorm():
296296
np.testing.assert_array_equal(1, mynorm(1.1))
297297
np.testing.assert_array_equal(4, mynorm(12))
298298

299+
# Test raises
300+
assert_raises(ValueError, mcolors.BoundaryNorm, bounds, cmref.N,
301+
extend='both', clip=True)
302+
299303
# Just min
300304
cmref = mcolors.ListedColormap(['blue', 'red'])
301305
cmref.set_under('white')

0 commit comments

Comments
 (0)
0