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

Skip to content

Commit 0346f1a

Browse files
authored
Merge pull request #6700 from anntzer/dont-cast-normalize-limits-to-float
FIX: Don't convert vmin, vmax to floats in norm
2 parents a18dd50 + ff78a06 commit 0346f1a

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

lib/matplotlib/colors.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -880,22 +880,13 @@ def process_value(value):
880880
Experimental; we may want to add an option to force the
881881
use of float32.
882882
"""
883-
if cbook.iterable(value):
884-
is_scalar = False
885-
result = np.ma.asarray(value)
886-
if result.dtype.kind == 'f':
887-
# this is overkill for lists of floats, but required
888-
# to support pd.Series as input until we can reliable
889-
# determine if result and value share memory in all cases
890-
# (list, tuple, deque, ndarray, Series, ...)
891-
result = result.copy()
892-
elif result.dtype.itemsize > 2:
893-
result = result.astype(float)
894-
else:
895-
result = result.astype(np.float32)
896-
else:
897-
is_scalar = True
898-
result = np.ma.array([value]).astype(float)
883+
is_scalar = not cbook.iterable(value)
884+
if is_scalar:
885+
value = [value]
886+
dtype = np.min_scalar_type(value)
887+
dtype = (np.float32 if dtype.itemsize <= 2
888+
else np.promote_types(dtype, float))
889+
result = np.ma.array(value, dtype=dtype, copy=True)
899890
return result, is_scalar
900891

901892
def __call__(self, value, clip=None):
@@ -918,8 +909,6 @@ def __call__(self, value, clip=None):
918909
elif vmin > vmax:
919910
raise ValueError("minvalue must be less than or equal to maxvalue")
920911
else:
921-
vmin = float(vmin)
922-
vmax = float(vmax)
923912
if clip:
924913
mask = np.ma.getmask(result)
925914
result = np.ma.array(np.clip(result.filled(vmax), vmin, vmax),
@@ -938,8 +927,7 @@ def __call__(self, value, clip=None):
938927
def inverse(self, value):
939928
if not self.scaled():
940929
raise ValueError("Not invertible until scaled")
941-
vmin = float(self.vmin)
942-
vmax = float(self.vmax)
930+
vmin, vmax = self.vmin, self.vmax
943931

944932
if cbook.iterable(value):
945933
val = np.ma.asarray(value)

lib/matplotlib/tests/test_colors.py

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

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

198208
def test_SymLogNorm():
199209
"""

0 commit comments

Comments
 (0)