8000 Merge pull request #25258 from dstansby/pow-norm-doc · matplotlib/matplotlib@1b8ac91 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1b8ac91

Browse files
authored
Merge pull request #25258 from dstansby/pow-norm-doc
Document PowerNorm parameters
2 parents c630505 + a225aa8 commit 1b8ac91

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

lib/matplotlib/colors.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,8 +1214,8 @@ def __init__(self, vmin=None, vmax=None, clip=False):
12141214
are mapped to 0 or 1, whichever is closer, and masked values are
12151215
set to 1. If ``False`` masked values remain masked.
12161216
1217-
Clipping silently defeats the purpose of setting the over, under,
1218-
and masked colors in a colormap, so it is likely to lead to
1217+
Clipping silently defeats the purpose of setting the over and
1218+
under colors in a colormap, so it is likely to lead to
12191219
surprises; therefore the default is ``clip=False``.
12201220
12211221
Notes
@@ -1775,8 +1775,8 @@ def forward(values: array-like) -> array-like
17751775
are mapped to 0 or 1, whichever is closer, and masked values are
17761776
set to 1. If ``False`` masked values remain masked.
17771777
1778-
Clipping silently defeats the purpose of setting the over, under,
1779-
and masked colors in a colormap, so it is likely to lead to
1778+
Clipping silently defeats the purpose of setting the over and
1779+
under colors in a colormap, so it is likely to lead to
17801780
surprises; therefore the default is ``clip=False``.
17811781
"""
17821782

@@ -1858,9 +1858,34 @@ def linear_width(self, value):
18581858

18591859

18601860
class PowerNorm(Normalize):
1861-
"""
1861+
r"""
18621862
Linearly map a given value to the 0-1 range and then apply
18631863
a power-law normalization over that range.
1864+
1865+
Parameters
1866+
----------
1867+
gamma : float
1868+
Power law exponent.
1869+
vmin, vmax : float or None
1870+
If *vmin* and/or *vmax* is not given, they are initialized from the
1871+
minimum and maximum value, respectively, of the first input
1872+
processed; i.e., ``__call__(A)`` calls ``autoscale_None(A)``.
1873+
clip : bool, default: False
1874+
If ``True`` values falling outside the range ``[vmin, vmax]``,
1875+
are mapped to 0 or 1, whichever is closer, and masked values
1876+
remain masked.
1877+
1878+
Clipping silently defeats the purpose of setting the over and under
1879+
colors, so it is likely to lead to surprises; therefore the default
1880+
is ``clip=False``.
1881+
1882+
Notes
1883+
-----
1884+
The normalization formula is
1885+
1886+
.. math::
1887+
1888+
\left ( \frac{x - v_{min}}{v_{max} - v_{min}} \right )^{\gamma}
18641889
"""
18651890
def __init__(self, gamma, vmin=None, vmax=None, clip=False):
18661891
super().__init__(vmin, vmax, clip)

lib/matplotlib/tests/test_colors.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,9 @@ def test_LogNorm_inverse():
545545

546546

547547
def test_PowerNorm():
548+
# Check an exponent of 1 gives same results as a normal linear
549+
# normalization. Also implicitly checks that vmin/vmax are
550+
# automatically initialized from first array input.
548551
a = np.array([0, 0.5, 1, 1.5], dtype=float)
549552
pnorm = mcolors.PowerNorm(1)
550553
norm = mcolors.Normalize()
@@ -561,19 +564,22 @@ def test_PowerNorm():
561564
# Clip = True
562565
a = np.array([-0.5, 0, 1, 8, 16], dtype=float)
563566
expected = [0, 0, 0, 1, 1]
567+
# Clip = True when creating the norm
564568
pnorm = mcolors.PowerNorm(2, vmin=2, vmax=8, clip=True)
565569
assert_array_almost_equal(pnorm(a), expected)
566570
assert pnorm(a[0]) == expected[0]
567571
assert pnorm(a[-1]) == expected[-1]
568-
569572
# Clip = True at call time
570-
a = np.array([-0.5, 0, 1, 8, 16], dtype=float)
571-
expected = [0, 0, 0, 1, 1]
572573
pnorm = mcolors.PowerNorm(2, vmin=2, vmax=8, clip=False)
573574
assert_array_almost_equal(pnorm(a, clip=True), expected)
574575
assert pnorm(a[0], clip=True) == expected[0]
575576
assert pnorm(a[-1], clip=True) == expected[-1]
576577

578+
# Check clip=True preserves mask 6167 ed values
579+
a = np.ma.array([5, 2], mask=[True, False])
580+
out = pnorm(a, clip=True)
581+
assert_array_equal(out.mask, [True, False])
582+
577583

578584
def test_PowerNorm_translation_invariance():
579585
a = np.array([0, 1/2, 1], dtype=float)

0 commit comments

Comments
 (0)
0