Skip to content

PiecewiseLinearNorm #5061

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
Closed
Prev Previous commit
Next Next commit
OffsetNorm -> PiecewiseLinearNorm
  • Loading branch information
dopplershift authored and OceanWolf committed Sep 13, 2015
commit 36042b6c800afab3f419b9b513069e4a1107f76e
2 changes: 1 addition & 1 deletion doc/users/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ determines the placement of the arrow head along the quiver line.

Offset Normalizers for Colormaps
````````````````````````````````
Paul Hobson/Geosyntec Consultants added a new :class:`matplotlib.colors.OffsetNorm`
Paul Hobson/Geosyntec Consultants added a new :class:`matplotlib.colors.PiecewiseLinearNorm`
class with the help of Till Stensitzki. This is particularly useful when using a
diverging colormap on data that are asymetrically centered around a logical value
(e.g., 0 when data range from -2 to 4).
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ def scaled(self):
return (self.vmin is not None and self.vmax is not None)


class OffsetNorm(Normalize):
class PiecewiseLinearNorm(Normalize):
"""
A subclass of matplotlib.colors.Normalize.

Expand Down Expand Up @@ -998,7 +998,7 @@ def __init__(self, vmin=None, vcenter=None, vmax=None, clip=False):
Examples
--------
>>> import matplotlib.colors as mcolors
>>> offset = mcolors.OffsetNorm(vmin=-2., vcenter=0., vmax=4.)
>>> offset = mcolors.PiecewiseLinearNorm(vmin=-2., vcenter=0., vmax=4.)
>>> data = [-2., -1., 0., 1., 2., 3., 4.]
>>> offset(data)
array([0., 0.25, 0.5, 0.625, 0.75, 0.875, 1.0])
Expand Down
46 changes: 23 additions & 23 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,44 +227,44 @@ def test_process_value_array(self):
assert_array_equal(res, np.array([5., 10.]))


class BaseOffsetNorm(BaseNormMixin):
normclass = mcolors.OffsetNorm
class BasePiecewiseLinearNorm(BaseNormMixin):
normclass = mcolors.PiecewiseLinearNorm
test_inverse = False

class test_OffsetNorm_Even(BaseOffsetNorm):
class test_PiecewiseLinearNorm_Even(BasePiecewiseLinearNorm):
def setup(self):
self.norm = self.normclass(vmin=-1, vcenter=0, vmax=4)
self.vals = np.array([-1.0, -0.5, 0.0, 1.0, 2.0, 3.0, 4.0])
self.expected = np.array([0.0, 0.25, 0.5, 0.625, 0.75, 0.875, 1.0])


class test_OffsetNorm_Odd(BaseOffsetNorm):
class test_PiecewiseLinearNorm_Odd(BasePiecewiseLinearNorm):
def setup(self):
self.normclass = mcolors.OffsetNorm
self.normclass = mcolors.PiecewiseLinearNorm
self.norm = self.normclass(vmin=-2, vcenter=0, vmax=5)
self.vals = np.array([-2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0])
self.expected = np.array([0.0, 0.25, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])


class test_OffsetNorm_AllNegative(BaseOffsetNorm):
class test_PiecewiseLinearNorm_AllNegative(BasePiecewiseLinearNorm):
def setup(self):
self.normclass = mcolors.OffsetNorm
self.normclass = mcolors.PiecewiseLinearNorm
self.norm = self.normclass(vmin=-10, vcenter=-8, vmax=-2)
self.vals = np.array([-10., -9., -8., -6., -4., -2.])
self.expected = np.array([0.0, 0.25, 0.5, 0.666667, 0.833333, 1.0])


class test_OffsetNorm_AllPositive(BaseOffsetNorm):
class test_PiecewiseLinearNorm_AllPositive(BasePiecewiseLinearNorm):
def setup(self):
self.normclass = mcolors.OffsetNorm
self.normclass = mcolors.PiecewiseLinearNorm
self.norm = self.normclass(vmin=0, vcenter=3, vmax=9)
self.vals = np.array([0., 1.5, 3., 4.5, 6.0, 7.5, 9.])
self.expected = np.array([0.0, 0.25, 0.5, 0.625, 0.75, 0.875, 1.0])


class test_OffsetNorm_NoVs(BaseOffsetNorm):
class test_PiecewiseLinearNorm_NoVs(BasePiecewiseLinearNorm):
def setup(self):
self.normclass = mcolors.OffsetNorm
self.normclass = mcolors.PiecewiseLinearNorm
self.norm = self.normclass(vmin=None, vcenter=None, vmax=None)
self.vals = np.array([-2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0])
self.expected = np.array([0., 0.16666667, 0.33333333,
Expand All @@ -289,26 +289,26 @@ def test_vmax(self):
assert_equal(self.norm.vmax, self.expected_vmax)


class test_OffsetNorm_VminEqualsVcenter(BaseOffsetNorm):
class test_PiecewiseLinearNorm_VminEqualsVcenter(BasePiecewiseLinearNorm):
def setup(self):
self.normclass = mcolors.OffsetNorm
self.normclass = mcolors.PiecewiseLinearNorm
self.norm = self.normclass(vmin=-2, vcenter=-2, vmax=2)
self.vals = np.array([-2.0, -1.0, 0.0, 1.0, 2.0])
self.expected = np.array([0.5, 0.625, 0.75, 0.875, 1.0])


class test_OffsetNorm_VmaxEqualsVcenter(BaseOffsetNorm):
class test_PiecewiseLinearNorm_VmaxEqualsVcenter(BasePiecewiseLinearNorm):
def setup(self):
self.normclass = mcolors.OffsetNorm
self.normclass = mcolors.PiecewiseLinearNorm
self.norm = self.normclass(vmin=-2, vcenter=2, vmax=2)
self.vals = np.array([-2.0, -1.0, 0.0, 1.0, 2.0])
self.expected = np.array([0.0, 0.125, 0.25, 0.375, 0.5])


class test_OffsetNorm_VsAllEqual(BaseOffsetNorm):
class test_PiecewiseLinearNorm_VsAllEqual(BasePiecewiseLinearNorm):
def setup(self):
self.v = 10
self.normclass = mcolors.OffsetNorm
self.normclass = mcolors.PiecewiseLinearNorm
self.norm = self.normclass(vmin=self.v, vcenter=self.v, vmax=self.v)
self.vals = np.array([-2.0, -1.0, 0.0, 1.0, 2.0])
self.expected = np.array([0.0, 0.0, 0.0, 0.0, 0.0])
Expand All @@ -321,28 +321,28 @@ def test_inverse(self):
)


class test_OffsetNorm_Errors(object):
class test_PiecewiseLinearNorm_Errors(object):
def setup(self):
self.vals = np.arange(50)

@raises(ValueError)
def test_VminGTVcenter(self):
norm = mcolors.OffsetNorm(vmin=10, vcenter=0, vmax=20)
norm = mcolors.PiecewiseLinearNorm(vmin=10, vcenter=0, vmax=20)
norm(self.vals)

@raises(ValueError)
def test_VminGTVmax(self):
norm = mcolors.OffsetNorm(vmin=10, vcenter=0, vmax=5)
norm = mcolors.PiecewiseLinearNorm(vmin=10, vcenter=0, vmax=5)
norm(self.vals)

@raises(ValueError)
def test_VcenterGTVmax(self):
norm = mcolors.OffsetNorm(vmin=10, vcenter=25, vmax=20)
norm = mcolors.PiecewiseLinearNorm(vmin=10, vcenter=25, vmax=20)
norm(self.vals)

@raises(ValueError)
def test_premature_scaling(self):
norm = mcolors.OffsetNorm()
norm = mcolors.PiecewiseLinearNorm()
norm.inverse(np.array([0.1, 0.5, 0.9]))


Expand All @@ -355,7 +355,7 @@ def test_offset_norm_img():

fig, (ax1, ax2) = plt.subplots(ncols=2)
cmap = plt.cm.coolwarm
norm = mcolors.OffsetNorm(vmin=-2, vcenter=0, vmax=7)
norm = mcolors.PiecewiseLinearNorm(vmin=-2, vcenter=0, vmax=7)

img1 = ax1.imshow(Z, cmap=cmap, norm=None)
cbar1 = fig.colorbar(img1, ax=ax1)
Expand Down