Skip to content

Commit c7d7d19

Browse files
committed
Merge pull request #6700 from anntzer/dont-cast-normalize-limits-to-float
FIX: Don't convert vmin, vmax to floats in norm Conflicts: lib/matplotlib/colors.py Kept version from master. Conflicts due to maskedarray name normalization.
1 parent 03775bb commit c7d7d19

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

lib/matplotlib/colors.py

+8-20
Original file line numberDiff line numberDiff line change
@@ -807,22 +807,13 @@ def process_value(value):
807807
Experimental; we may want to add an option to force the
808808
use of float32.
809809
"""
810-
if cbook.iterable(value):
811-
is_scalar = False
812-
result = ma.asarray(value)
813-
if result.dtype.kind == 'f':
814-
# this is overkill for lists of floats, but required
815-
# to support pd.Series as input until we can reliable
816-
# determine if result and value share memory in all cases
817-
# (list, tuple, deque, ndarray, Series, ...)
818-
result = result.copy()
819-
elif result.dtype.itemsize > 2:
820-
result = result.astype(np.float)
821-
else:
822-
result = result.astype(np.float32)
823-
else:
824-
is_scalar = True
825-
result = ma.array([value]).astype(np.float)
810+
is_scalar = not cbook.iterable(value)
811+
if is_scalar:
812+
value = [value]
813+
dtype = np.min_scalar_type(value)
814+
dtype = (np.float32 if dtype.itemsize <= 2
815+
else np.promote_types(dtype, float))
816+
result = np.ma.array(value, dtype=dtype, copy=True)
826817
return result, is_scalar
827818

828819
def __call__(self, value, clip=None):
@@ -845,8 +836,6 @@ def __call__(self, value, clip=None):
845836
elif vmin > vmax:
846837
raise ValueError("minvalue must be less than or equal to maxvalue")
847838
else:
848-
vmin = float(vmin)
849-
vmax = float(vmax)
850839
if clip:
851840
mask = ma.getmask(result)
852841
result = ma.array(np.clip(result.filled(vmax), vmin, vmax),
@@ -865,8 +854,7 @@ def __call__(self, value, clip=None):
865854
def inverse(self, value):
866855
if not self.scaled():
867856
raise ValueError("Not invertible until scaled")
868-
vmin = float(self.vmin)
869-
vmax = float(self.vmax)
857+
vmin, vmax = self.vmin, self.vmax
870858

871859
if cbook.iterable(value):
872860
val = ma.asarray(value)

lib/matplotlib/tests/test_colors.py

+10
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,16 @@ def test_Normalize():
193193
_scalar_tester(norm, vals)
194194
_mask_tester(norm, vals)
195195

196+
# Don't lose precision on longdoubles (float128 on Linux):
197+
# for array inputs...
198+
vals = np.array([1.2345678901, 9.8765432109], dtype=np.longdouble)
199+
norm = mcolors.Normalize(vals.min(), vals.max())
200+
assert_array_equal(np.asarray(norm(vals)), [0, 1])
201+
# and for scalar ones.
202+
eps = np.finfo(np.longdouble).resolution
203+
norm = plt.Normalize(1, 1 + 100 * eps)
204+
assert_equal(norm(1 + 50 * eps), .5)
205+
196206

197207
def test_SymLogNorm():
198208
"""

0 commit comments

Comments
 (0)