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

Skip to content

Commit 64351f5

Browse files
author
Nathan Goldbaum
committed
BUG: sanitize norm extrema to be floats
1 parent ef5d01a commit 64351f5

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

lib/matplotlib/cm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,9 @@ def set_clim(self, vmin=None, vmax=None):
314314
except (TypeError, ValueError):
315315
pass
316316
if vmin is not None:
317-
self.norm.vmin = vmin
317+
self.norm.vmin = colors._sanitize_extrema(vmin)
318318
if vmax is not None:
319-
self.norm.vmax = vmax
319+
self.norm.vmax = colors._sanitize_extrema(vmax)
320320
self.changed()
321321

322322
def set_cmap(self, cmap):

lib/matplotlib/colors.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ def get_named_colors_mapping():
9494
return _colors_full_map
9595

9696

97+
def _sanitize_extrema(ex):
98+
if ex is None:
99+
return ex
100+
try:
101+
ret = np.asscalar(ex)
102+
except AttributeError:
103+
ret = float(ex)
104+
return ret
105+
106+
97107
def _is_nth_color(c):
98108
"""Return whether *c* can be interpreted as an item in the color cycle."""
99109
return isinstance(c, six.string_types) and re.match(r"\AC[0-9]\Z", c)
@@ -878,8 +888,8 @@ def __init__(self, vmin=None, vmax=None, clip=False):
878888
likely to lead to surprises; therefore the default is
879889
*clip* = *False*.
880890
"""
881-
self.vmin = vmin
882-
self.vmax = vmax
891+
self.vmin = _sanitize_extrema(vmin)
892+
self.vmax = _sanitize_extrema(vmax)
883893
self.clip = clip
884894

885895
@staticmethod

lib/matplotlib/tests/test_colors.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,12 +686,21 @@ def __isub__(self, other):
686686
def __add__(self, other):
687687
raise RuntimeError
688688

689+
def __getitem__(self, index):
690+
ret = super(MyArray, self).__getitem__(index)
691+
if ret.shape == ():
692+
ret = np.array(ret)
693+
return ret.view(MyArray)
694+
689695
data = np.arange(-10, 10, 1, dtype=float)
696+
mydata = data.view(MyArray)
690697

691698
for norm in [mcolors.Normalize(), mcolors.LogNorm(),
692699
mcolors.SymLogNorm(3, vmax=5, linscale=1),
700+
mcolors.Normalize(vmin=mydata.max(), vmax=mydata.max()),
701+
mcolors.SymLogNorm(3, vmin=mydata.max(), vmax=mydata.max()),
693702
mcolors.PowerNorm(1)]:
694-
assert_array_equal(norm(data.view(MyArray)), norm(data))
703+
assert_array_equal(norm(mydata), norm(data))
695704
if isinstance(norm, mcolors.PowerNorm):
696705
assert len(recwarn) == 1
697706
warn = recwarn.pop(UserWarning)

0 commit comments

Comments
 (0)