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

Skip to content

Commit a7747e5

Browse files
authored
Merge pull request #10730 from matplotlib/auto-backport-of-pr-10721
Backport PR #10721 on branch v2.2.x
2 parents 5011605 + 1d9ef31 commit a7747e5

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
@@ -705,11 +705,18 @@ def __add__(self, other):
705705
raise RuntimeError
706706

707707
data = np.arange(-10, 10, 1, dtype=float)
708+
data.shape = (10, 2)
709+
mydata = data.view(MyArray)
708710

709711
for norm in [mcolors.Normalize(), mcolors.LogNorm(),
710712
mcolors.SymLogNorm(3, vmax=5, linscale=1),
713+
mcolors.Normalize(vmin=mydata.min(), vmax=mydata.max()),
714+
mcolors.SymLogNorm(3, vmin=mydata.min(), vmax=mydata.max()),
711715
mcolors.PowerNorm(1)]:
712-
assert_array_equal(norm(data.view(MyArray)), norm(data))
716+
assert_array_equal(norm(mydata), norm(data))
717+
fig, ax = plt.subplots()
718+
ax.imshow(mydata, norm=norm)
719+
fig.canvas.draw()
713720
if isinstance(norm, mcolors.PowerNorm):
714721
assert len(recwarn) == 1
715722
warn = recwarn.pop(UserWarning)

0 commit comments

Comments
 (0)