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

Skip to content

Commit ff78a06

Browse files
committed
Proper norm'ing of float128 scalars too.
1 parent 16f9778 commit ff78a06

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

lib/matplotlib/colors.py

Lines changed: 7 additions & 16 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):

lib/matplotlib/tests/test_colors.py

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

197-
# Don't lose precision on longdoubles (float128 on Linux).
197+
# Don't lose precision on longdoubles (float128 on Linux):
198+
# for array inputs...
198199
vals = np.array([1.2345678901, 9.8765432109], dtype=np.longdouble)
199200
norm = mcolors.Normalize(vals.min(), vals.max())
200201
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)
201206

202207

203208
def test_SymLogNorm():

0 commit comments

Comments
 (0)