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

Skip to content

Commit f5f5890

Browse files
committed
Allows one to not clip to 0...1 in colors.MultivarColormap.__call__()
Also adds a an improvement to colors.BIvarColormap.__getitem__() so that this returns a ListedColormap object instead of a Colormap object
1 parent cdc782d commit f5f5890

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

lib/matplotlib/colors.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ def __init__(self, name, colormaps, combination_mode):
12951295
self.n_variates = len(colormaps)
12961296
self._rgba_bad = (0.0, 0.0, 0.0, 0.0) # If bad, don't paint anything.
12971297

1298-
def __call__(self, X, alpha=None, bytes=False):
1298+
def __call__(self, X, alpha=None, bytes=False, clip=True):
12991299
r"""
13001300
Parameters
13011301
----------
@@ -1315,6 +1315,8 @@ def __call__(self, X, alpha=None, bytes=False):
13151315
If False (default), the returned RGBA values will be floats in the
13161316
interval ``[0, 1]`` otherwise they will be `numpy.uint8`\s in the
13171317
interval ``[0, 255]``.
1318+
clip : bool
1319+
If True, clip output to 0 to 1
13181320
13191321
Returns
13201322
-------
@@ -1340,17 +1342,24 @@ def __call__(self, X, alpha=None, bytes=False):
13401342

13411343
rgba[mask_bad] = self.get_bad()
13421344

1343-
rgba = np.clip(rgba, 0, 1)
1345+
if clip:
1346+
rgba = np.clip(rgba, 0, 1)
13441347

13451348
if alpha is not None:
1346-
alpha = np.clip(alpha, 0, 1)
1349+
if clip:
1350+
alpha = np.clip(alpha, 0, 1)
13471351
if alpha.shape not in [(), np.array(X[0]).shape]:
13481352
raise ValueError(
13491353
f"alpha is array-like but its shape {alpha.shape} does "
13501354
f"not match that of X[0] {np.array(X[0]).shape}")
13511355
rgba[..., -1] *= alpha
13521356

13531357
if bytes:
1358+
if not clip:
1359+
raise ValueError(
1360+
"clip cannot be false while bytes is true"
1361+
" as uint8 does not support values below 0"
1362+
" or above 255.")
13541363
rgba = (rgba * 255).astype('uint8')
13551364

13561365
if not np.iterable(X[0]):
@@ -1736,24 +1745,20 @@ def __getitem__(self, item):
17361745
if not self._isinit:
17371746
self._init()
17381747
if item == 0:
1739-
cmap = Colormap(self.name+'0', self.N)
17401748
one_d_lut = self._lut[:, self._origin[1]]
1749+
new_cmap = ListedColormap(one_d_lut, name=self.name+'_0', N=self.N)
1750+
17411751
elif item == 1:
1742-
cmap = Colormap(self.name+'1', self.M)
17431752
one_d_lut = self._lut[self._origin[0], :]
1753+
new_cmap = ListedColormap(one_d_lut, name=self.name+'_1', N=self.M)
17441754
else:
17451755
raise KeyError(f"only 0 or 1 are"
17461756
f" valid keys for BivarColormap, not {item!r}")
1747-
cmap._lut = np.zeros((self.N + 3, 4), float)
1748-
cmap._lut[:-3] = one_d_lut
1749-
cmap.set_bad(self._rgba_bad)
1750-
self._rgba_outside
1757+
new_cmap._rgba_bad = self._rgba_bad
17511758
if self.shape in ['ignore', 'circleignore']:
1752-
cmap.set_under(self._rgba_outside)
1753-
cmap.set_over(self._rgba_outside)
1754-
cmap._set_extremes()
1755-
cmap._isinit = True
1756-
return cmap
1759+
new_cmap.set_over(self._rgba_outside)
1760+
new_cmap.set_under(self._rgba_outside)
1761+
return new_cmap
17571762

17581763
def _repr_png_(self):
17591764
"""Generate a PNG representation of the BivarColormap."""

0 commit comments

Comments
 (0)