10BC0 Fix Normalize(<signed integer array>). · matplotlib/matplotlib@1a2c4b0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1a2c4b0

Browse files
committed
< 10BC0 /div>
Fix Normalize(<signed integer array>).
Basically, everything needs to be cast to a float early enough.
1 parent 80a3f3e commit 1a2c4b0

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lib/matplotlib/colors.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,9 @@ def __call__(self, value, clip=None):
903903
result, is_scalar = self.process_value(value)
904904

905905
self.autoscale_None(result)
906-
vmin, vmax = self.vmin, self.vmax
906+
# Convert at least to float, without losing precision.
907+
(vmin,), _ = self.process_value(self.vmin)
908+
(vmax,), _ = self.process_value(self.vmax)
907909
if vmin == vmax:
908910
result.fill(0) # Or should it be all masked? Or 0.5?
909911
elif vmin > vmax:
@@ -927,7 +929,8 @@ def __call__(self, value, clip=None):
927929
def inverse(self, value):
928930
if not self.scaled():
929931
raise ValueError("Not invertible until scaled")
930-
vmin, vmax = self.vmin, self.vmax
932+
(vmin,), _ = self.process_value(self.vmin)
933+
(vmax,), _ = self.process_value(self.vmax)
931934

932935
if cbook.iterable(value):
933936
val = np.ma.asarray(value)

lib/matplotlib/tests/test_colors.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ def test_Normalize():
194194
_scalar_tester(norm, vals)
195195
_mask_tester(norm, vals)
196196

197+
# Handle integer input correctly (don't overflow when computing max-min,
198+
# i.e. 127-(-128) here).
199+
vals = np.array([-128, 127], dtype=np.int8)
200+
norm = mcolors.Normalize(vals.min(), vals.max())
201+
assert_array_equal(np.asarray(norm(vals)), [0, 1])
202+
197203
# Don't lose precision on longdoubles (float128 on Linux):
198204
# for array inputs...
199205
vals = np.array([1.2345678901, 9.8765432109], dtype=np.longdouble)

0 commit comments

Comments
 (0)
0