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

Skip to content

Commit c85b029

Browse files
authored
Merge pull request #10721 from ngoldbaum/norm-unit-fix
sanitize norm extrema to be floats
2 parents ef5d01a + fd40f1b commit c85b029

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,11 +687,18 @@ def __add__(self, other):
687687
raise RuntimeError
688688

689689
data = np.arange(-10, 10, 1, dtype=float)
690+
data.shape = (10, 2)
691+
mydata = data.view(MyArray)
690692

691693
for norm in [mcolors.Normalize(), mcolors.LogNorm(),
692694
mcolors.SymLogNorm(3, vmax=5, linscale=1),
695+
mcolors.Normalize(vmin=mydata.min(), vmax=mydata.max()),
696+
mcolors.SymLogNorm(3, vmin=mydata.min(), vmax=mydata.max()),
693697
mcolors.PowerNorm(1)]:
694-
assert_array_equal(norm(data.view(MyArray)), norm(data))
698+
assert_array_equal(norm(mydata), norm(data))
699+
fig, ax = plt.subplots()
700+
ax.imshow(mydata, norm=norm)
701+
fig.canvas.draw()
695702
if isinstance(norm, mcolors.PowerNorm):
696703
assert len(recwarn) == 1
697704
warn = recwarn.pop(UserWarning)

0 commit comments

Comments
 (0)