Skip to content

Commit fdfcbeb

Browse files
authored
Merge pull request #15787 from anntzer/colormap-call
Cleanup `Colormap.__call__`.
2 parents c9a551d + 1ae4400 commit fdfcbeb

File tree

1 file changed

+14
-30
lines changed

1 file changed

+14
-30
lines changed

lib/matplotlib/colors.py

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -550,46 +550,30 @@ def __call__(self, X, alpha=None, bytes=False):
550550
-------
551551
Tuple of RGBA values if X is scalar, otherwise an array of
552552
RGBA values with a shape of ``X.shape + (4, )``.
553-
554553
"""
555-
# See class docstring for arg/kwarg documentation.
556554
if not self._isinit:
557555
self._init()
558-
mask_bad = None
559-
if np.ma.is_masked(X):
560-
mask_bad = X.mask
561-
elif np.any(np.isnan(X)):
562-
# mask nan's
563-
mask_bad = np.isnan(X)
564556

557+
mask_bad = X.mask if np.ma.is_masked(X) else np.isnan(X) # Mask nan's.
565558
xa = np.array(X, copy=True)
566-
# Fill bad values to avoid warnings
567-
# in the boolean comparisons below.
568-
if mask_bad is not None:
569-
xa[mask_bad] = 0.
570-
571-
# Calculations with native byteorder are faster, and avoid a
572-
# bug that otherwise can occur with putmask when the last
573-
# argument is a numpy scalar.
574559
if not xa.dtype.isnative:
575-
xa = xa.byteswap().newbyteorder()
576-
560+
xa = xa.byteswap().newbyteorder() # Native byteorder is faster.
577561
if xa.dtype.kind == "f":
578-
xa *= self.N
579-
# Negative values are out of range, but astype(int) would truncate
580-
# them towards zero.
581-
xa[xa < 0] = -1
582-
# xa == 1 (== N after multiplication) is not out of range.
583-
xa[xa == self.N] = self.N - 1
584-
# Avoid converting large positive values to negative integers.
585-
np.clip(xa, -1, self.N, out=xa)
586-
xa = xa.astype(int)
562+
with np.errstate(invalid="ignore"):
563+
xa *= self.N
564+
# Negative values are out of range, but astype(int) would
565+
# truncate them towards zero.
566+
xa[xa < 0] = -1
567+
# xa == 1 (== N after multiplication) is not out of range.
568+
xa[xa == self.N] = self.N - 1
569+
# Avoid converting large positive values to negative integers.
570+
np.clip(xa, -1, self.N, out=xa)
571+
xa = xa.astype(int)
587572
# Set the over-range indices before the under-range;
588573
# otherwise the under-range values get converted to over-range.
589574
xa[xa > self.N - 1] = self._i_over
590575
xa[xa < 0] = self._i_under
591-
if mask_bad is not None:
592-
xa[mask_bad] = self._i_bad
576+
xa[mask_bad] = self._i_bad
593577

594578
if bytes:
595579
lut = (self._lut * 255).astype(np.uint8)
@@ -610,7 +594,7 @@ def __call__(self, X, alpha=None, bytes=False):
610594
# If the bad value is set to have a color, then we
611595
# override its alpha just as for any other value.
612596

613-
rgba = lut.take(xa, axis=0, mode='clip')
597+
rgba = lut[xa]
614598
if not np.iterable(X):
615599
# Return a tuple if the input was a scalar
616600
rgba = tuple(rgba)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy