8000 Simplify Colormap.__call__. · matplotlib/matplotlib@fea491b · GitHub
[go: up one dir, main page]

Skip to content

Commit fea491b

Browse files
committed
Simplify Colormap.__call__.
1 parent 96074c7 commit fea491b

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

lib/matplotlib/colors.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -487,24 +487,19 @@ def __call__(self, X, alpha=None, bytes=False):
487487
xa = xa.byteswap().newbyteorder()
488488

489489
if xa.dtype.kind == "f":
490-
# Treat 1.0 as slightly less than 1.
491-
vals = np.array([1, 0], dtype=xa.dtype)
492-
almost_one = np.nextafter(*vals)
493-
np.copyto(xa, almost_one, where=xa == 1.0)
494-
# The following clip is fast, and prevents possible
495-
# conversion of large positive values to negative integers.
496-
497490
xa *= self.N
491+
# Negative values are out of range, but astype(int) would truncate
492+
# them towards zero.
493+
xa[xa < 0] = -1
494+
# xa == 1 (== N after multiplication) is not out of range.
495+
xa[xa == self.N] = self.N - 1
496+
# Avoid converting large positive values to negative integers.
498497
np.clip(xa, -1, self.N, out=xa)
499-
500-
# ensure that all 'under' values will still have negative
501-
# value after casting to int
502-
np.copyto(xa, -1, where=xa < 0.0)
503498
xa = xa.astype(int)
504499
# Set the over-range indices before the under-range;
505500
# otherwise the under-range values get converted to over-range.
506-
np.copyto(xa, self._i_over, where=xa > self.N - 1)
507-
np.copyto(xa, self._i_under, where=xa < 0)
501+
xa[xa > self.N - 1] = self._i_over
502+
xa[xa < 0] = self._i_under
508503
if mask_bad is not None:
509504
if mask_bad.shape == xa.shape:
510505
np.copyto(xa, self._i_bad, where=mask_bad)

0 commit comments

Comments
 (0)
0