Skip to content

[NF] Add 'truncate' and 'join' methods to colormaps. #7716

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

Closed
wants to merge 18 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Some pep8 fixes.
  • Loading branch information
lkilcher committed May 17, 2018
commit e15203affb341cde8f013182aa2f4ad14342d466
26 changes: 15 additions & 11 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ def to_rgba_array(c, alpha=None):
# Special-case inputs that are already arrays, for performance. (If the
# array has the wrong kind or shape, raise the error during one-at-a-time
# conversion.)
if (isinstance(c, np.ndarray) and c.dtype.kind in "if"
and c.ndim == 2 and c.shape[1] in [3, 4]):
if (isinstance(c, np.ndarray) and c.dtype.kind in "if" and
c.ndim == 2 and c.shape[1] in [3, 4]):
if c.shape[1] == 3:
result = np.column_stack([c, np.zeros(len(c))])
result[:, -1] = alpha if alpha is not None else 1.
Expand Down Expand Up @@ -799,7 +799,7 @@ def join(self, other, name=None, frac_self=None, N=None):
if name is None:
name = '{}+{}'.format(self.name, other.name)
assert 0 < frac_self and frac_self < 1, (
"The parameter ``frac_self`` must be in the interval ``(0.0, 1.0)``."
"The parameter frac_self must be in the interval (0.0, 1.0)."
)
map0 = self(np.linspace(0, 1, int(N * frac_self)))
map1 = other(np.linspace(0, 1, int(N * (1 - frac_self))))
Expand Down Expand Up @@ -840,11 +840,11 @@ def truncate(self, minval=0.0, maxval=1.0, N=None):
cmap_trunc = cmap.truncate(0.2, 0.7)

"""
assert minval < maxval, "``minval`` must be less than ``maxval``"
assert minval < maxval, "minval must be less than maxval"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use if not cond: raise instead of asserts? A while ago we went through and removed all of the run-time uses of asserts for input validation as (in principle) the interpreter can decide to ignore the assert statements.

assert 0 <= minval and minval < 1, (
"The parameter ``minval`` must be in the interval ``(0.0, 1.0)``.")
"The parameter minval must be in the interval (0.0, 1.0).")
assert 0 < maxval and maxval <= 1, (
"The parameter ``maxval`` must be in the interval ``(0.0, 1.0)``.")
"The parameter maxval must be in the interval (0.0, 1.0).")
assert minval != 0 or maxval != 1, (
"This is not a truncation.")
# This was taken largely from
Expand All @@ -854,7 +854,11 @@ def truncate(self, minval=0.0, maxval=1.0, N=None):
if N is None:
N = np.ceil(self.N * (maxval - minval))
name = "trunc({},{:.2f},{:.2f})".format(self.name, minval, maxval)
return LinearSegmentedColormap.from_list(name, self(np.linspace(minval, maxval, N)), N)
return LinearSegmentedColormap.from_list(name,
self(np.linspace(minval,
maxval,
N)),
N)


class ListedColormap(Colormap):
Expand Down Expand Up @@ -982,7 +986,7 @@ def join(self, other, name=None, frac_self=None, N=None):
if name is None:
name = '{}+{}'.format(self.name, other.name)
assert 0 < frac_self and frac_self < 1, (
"The parameter ``frac_self`` must be in the interval ``(0.0, 1.0)``."
"The parameter frac_self must be in the interval (0.0, 1.0)."
)
map0 = self(np.linspace(0, 1, int(N * frac_self)))
map1 = other(np.linspace(0, 1, int(N * (1 - frac_self))))
Expand Down Expand Up @@ -1023,11 +1027,11 @@ def truncate(self, minval=0.0, maxval=1.0, N=None):
cmap_trunc = cmap.truncate(0.2, 0.7)

"""
assert minval < maxval, "``minval`` must be less than ``maxval``"
assert minval < maxval, "minval must be less than maxval"
assert 0 <= minval and minval < 1, (
"The parameter ``minval`` must be in the interval ``(0.0, 1.0)``.")
"The parameter minval must be in the interval (0.0, 1.0).")
assert 0 < maxval and maxval <= 1, (
"The parameter ``maxval`` must be in the interval ``(0.0, 1.0)``.")
"The parameter maxval must be in the interval (0.0, 1.0).")
assert minval != 0 or maxval != 1, (
"This is not a truncation.")
# This was taken largely from
Expand Down