Skip to content

sanitize norm extrema to be floats #10721

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 1 commit into from
Mar 9, 2018
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
4 changes: 2 additions & 2 deletions lib/matplotlib/cm.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,9 @@ def set_clim(self, vmin=None, vmax=None):
except (TypeError, ValueError):
pass
if vmin is not None:
self.norm.vmin = vmin
self.norm.vmin = colors._sanitize_extrema(vmin)
if vmax is not None:
self.norm.vmax = vmax
self.norm.vmax = colors._sanitize_extrema(vmax)
self.changed()

def set_cmap(self, cmap):
Expand Down
14 changes: 12 additions & 2 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ def get_named_colors_mapping():
return _colors_full_map


def _sanitize_extrema(ex):
if ex is None:
return ex
try:
ret = np.asscalar(ex)
except AttributeError:
ret = float(ex)
return ret


def _is_nth_color(c):
"""Return whether *c* can be interpreted as an item in the color cycle."""
return isinstance(c, six.string_types) and re.match(r"\AC[0-9]\Z", c)
Expand Down Expand Up @@ -878,8 +888,8 @@ def __init__(self, vmin=None, vmax=None, clip=False):
likely to lead to surprises; therefore the default is
*clip* = *False*.
"""
self.vmin = vmin
self.vmax = vmax
self.vmin = _sanitize_extrema(vmin)
self.vmax = _sanitize_extrema(vmax)
self.clip = clip

@staticmethod
Expand Down
9 changes: 8 additions & 1 deletion lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,11 +687,18 @@ def __add__(self, other):
raise RuntimeError

data = np.arange(-10, 10, 1, dtype=float)
data.shape = (10, 2)
mydata = data.view(MyArray)

for norm in [mcolors.Normalize(), mcolors.LogNorm(),
mcolors.SymLogNorm(3, vmax=5, linscale=1),
mcolors.Normalize(vmin=mydata.min(), vmax=mydata.max()),
mcolors.SymLogNorm(3, vmin=mydata.min(), vmax=mydata.max()),
mcolors.PowerNorm(1)]:
assert_array_equal(norm(data.view(MyArray)), norm(data))
assert_array_equal(norm(mydata), norm(data))
fig, ax = plt.subplots()
ax.imshow(mydata, norm=norm)
fig.canvas.draw()
if isinstance(norm, mcolors.PowerNorm):
assert len(recwarn) == 1
warn = recwarn.pop(UserWarning)
Expand Down