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

Skip to content

Commit f927517

Browse files
committed
Merge pull request #1095 from efiring/colormap_byteorder_bug
Colormap byteorder bug
2 parents 2a25fe8 + 64e98e6 commit f927517

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

lib/matplotlib/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,7 @@ def tk_window_focus():
10081008
'matplotlib.tests.test_basic',
10091009
'matplotlib.tests.test_cbook',
10101010
'matplotlib.tests.test_colorbar',
1011+
'matplotlib.tests.test_colors',
10111012
'matplotlib.tests.test_dates',
10121013
'matplotlib.tests.test_delaunay',
10131014
'matplotlib.tests.test_figure',

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

lib/matplotlib/tests/test_colors.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
Tests for the colors module.
3+
"""
4+
5+
from __future__ import print_function
6+
import numpy as np
7+
from numpy.testing.utils import assert_array_equal
8+
import matplotlib.colors as mcolors
9+
import matplotlib.cm as cm
10+
11+
def test_colormap_endian():
12+
"""
13+
Github issue #1005: a bug in putmask caused erroneous
14+
mapping of 1.0 when input from a non-native-byteorder
15+
array.
16+
"""
17+
cmap = cm.get_cmap("jet")
18+
# Test under, over, and invalid along with values 0 and 1.
19+
a = [-0.5, 0, 0.5, 1, 1.5, np.nan]
20+
for dt in ["f2", "f4", "f8"]:
21+
anative = np.ma.masked_invalid(np.array(a, dtype=dt))
22+
aforeign = anative.byteswap().newbyteorder()
23+
#print(anative.dtype.isnative, aforeign.dtype.isnative)
24+
assert_array_equal(cmap(anative), cmap(aforeign))
25+
26+

0 commit comments

Comments
 (0)