Skip to content

Don't convert vmin, vmax to floats. #6700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 8 additions & 20 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,22 +880,13 @@ def process_value(value):
Experimental; we may want to add an option to force the
use of float32.
"""
if cbook.iterable(value):
is_scalar = False
result = np.ma.asarray(value)
if result.dtype.kind == 'f':
# this is overkill for lists of floats, but required
# to support pd.Series as input until we can reliable
# determine if result and value share memory in all cases
# (list, tuple, deque, ndarray, Series, ...)
result = result.copy()
elif result.dtype.itemsize > 2:
result = result.astype(float)
else:
result = result.astype(np.float32)
else:
is_scalar = True
result = np.ma.array([value]).astype(float)
is_scalar = not cbook.iterable(value)
if is_scalar:
value = [value]
dtype = np.min_scalar_type(value)
dtype = (np.float32 if dtype.itemsize <= 2
else np.promote_types(dtype, float))
result = np.ma.array(value, dtype=dtype, copy=True)
return result, is_scalar

def __call__(self, value, clip=None):
Expand All @@ -918,8 +909,6 @@ def __call__(self, value, clip=None):
elif vmin > vmax:
raise ValueError("minvalue must be less than or equal to maxvalue")
else:
vmin = float(vmin)
vmax = float(vmax)
if clip:
mask = np.ma.getmask(result)
result = np.ma.array(np.clip(result.filled(vmax), vmin, vmax),
Expand All @@ -938,8 +927,7 @@ def __call__(self, value, clip=None):
def inverse(self, value):
if not self.scaled():
raise ValueError("Not invertible until scaled")
vmin = float(self.vmin)
vmax = float(self.vmax)
vmin, vmax = self.vmin, self.vmax

if cbook.iterable(value):
val = np.ma.asarray(value)
Expand Down
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,16 @@ def test_Normalize():
_scalar_tester(norm, vals)
_mask_tester(norm, vals)

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


def test_SymLogNorm():
"""
Expand Down