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

Skip to content

Commit a99651e

Browse files
committed
Colormap: ensure native byteorder to avoid obscure putmask bug, #1005
1 parent 551f11b commit a99651e

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

lib/matplotlib/colors.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,10 +515,17 @@ def __call__(self, X, alpha=None, bytes=False):
515515
xa = xma.filled() # Fill to avoid infs, etc.
516516
del xma
517517

518-
if xa.dtype.char in np.typecodes['Float']:
518+
# Calculations with native byteorder are faster, and avoid a
519+
# bug that otherwise can occur with putmask when the last
520+
# argument is a numpy scalar.
521+
if not xa.dtype.isnative:
522+
xa = xa.byteswap().newbyteorder()
523+
524+
if xa.dtype.kind == "f":
519525
# Treat 1.0 as slightly less than 1.
520-
cbook._putmask(xa, xa==1.0, np.nextafter(xa.dtype.type(1),
521-
xa.dtype.type(0)))
526+
vals = np.array([1, 0], dtype=xa.dtype)
527+
almost_one = np.nextafter(*vals)
528+
cbook._putmask(xa, xa==1.0, almost_one)
522529
# The following clip is fast, and prevents possible
523530
# conversion of large positive values to negative integers.
524531

0 commit comments

Comments
 (0)