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

Skip to content

Add PiecewiseLinearNorm #4666

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 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 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
25 changes: 10 additions & 15 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ class PiecewiseLinearNorm(Normalize):

Normalizes data into the ``[0.0, 1.0]`` interval.
"""
def __init__(self, vmin=None, vcenter=None, vmax=None, clip=False):
def __init__(self, vmin=None, vcenter=None, vmax=None):
"""Normalize data with an offset midpoint

Useful when mapping data unequally centered around a conceptual
Expand All @@ -990,11 +990,6 @@ def __init__(self, vmin=None, vcenter=None, vmax=None, clip=False):
The data value that defines ``1.0`` in the normalized data.
Defaults to the the max value of the dataset.

clip : bool, optional (default is False)
If *clip* is True, values beyond *vmin* and *vmax* will be set
to ``0.0`` or ``1.0``, respectively. Otherwise, values outside
the ``[0.0, 1.0]`` will be returned.

Examples
--------
>>> import matplotlib.colors as mcolors
Expand All @@ -1008,11 +1003,9 @@ def __init__(self, vmin=None, vcenter=None, vmax=None, clip=False):
self.vmin = vmin
self.vcenter = vcenter
self.vmax = vmax
self.clip = clip

def __call__(self, value, clip=None):
if clip is None:
clip = self.clip
"""Map value to the interval [0, 1]. The clip argument is unused."""

result, is_scalar = self.process_value(value)

Expand All @@ -1028,15 +1021,17 @@ def __call__(self, value, clip=None):
vmin = float(vmin)
vcenter = float(vcenter)
vmax = float(vmax)
if clip:
mask = ma.getmask(result)
result = ma.array(np.clip(result.filled(vmax), vmin, vmax),
mask=mask)
# in degenerate cases, prefer the center value to the extremes
degen = (result == vcenter) if vcenter == vmax else None

x, y = [vmin, vcenter, vmax], [0, 0.5, 1]
# returns a scalar if shape == (1,)
result = np.ma.masked_array(np.interp(value, x, y))
result = ma.masked_array(np.interp(result, x, y),
mask=ma.getmask(result))
if degen is not None:
result[degen] = 0.5

if is_scalar:
result = np.atleast_1d(result)[0]
return result

def autoscale_None(self, A):
Expand Down
2 changes: 2 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ class BasePiecewiseLinearNorm(BaseNormMixin):
normclass = mcolors.PiecewiseLinearNorm
test_inverse = False


class test_PiecewiseLinearNorm_Even(BasePiecewiseLinearNorm):
def setup(self):
self.norm = self.normclass(vmin=-1, vcenter=0, vmax=4)
Expand Down Expand Up @@ -298,6 +299,7 @@ def test_offset_norm_img():
img2 = ax2.imshow(Z, cmap=cmap, norm=norm)
cbar2 = fig.colorbar(img2, ax=ax2)


def test_SymLogNorm():
"""
Test SymLogNorm behavior
Expand Down