Skip to content

Commit 8c1fee8

Browse files
committed
FIX: add base kwarg to symlognor
1 parent 7b6eb77 commit 8c1fee8

File tree

6 files changed

+57
-18
lines changed

6 files changed

+57
-18
lines changed

doc/api/next_api_changes/behaviour.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,12 @@ of *y*, the second column of *x* against the second column of *y*, **and** the
7171
first column of *x* against the third column of *y*. This now raises an error
7272
instead.
7373

74-
`.Text.update_from` now copies usetex state from the source Text
74+
`.Text.update_from` now copies usetex state from the source Text
7575
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
76+
77+
`.SymLogNorm` now has a *base* kwarg.
78+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
79+
Previously, `SymLogNorm` had not *base* kwarg, and defaulted to ``base=np.e``
80+
whereas the documentation said it was ``base=10``. In preparation to make
81+
the default 10, calling `SymLogNorm` without the new *base* kwarg emits a
82+
deprecation warning.

examples/userdemo/colormap_normalizations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969

7070
pcm = ax[0].pcolormesh(X, Y, Z1,
7171
norm=colors.SymLogNorm(linthresh=0.03, linscale=0.03,
72-
vmin=-1.0, vmax=1.0),
72+
vmin=-1.0, vmax=1.0, base=10),
7373
cmap='RdBu_r')
7474
fig.colorbar(pcm, ax=ax[0], extend='both')
7575

examples/userdemo/colormap_normalizations_symlognorm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
pcm = ax[0].pcolormesh(X, Y, Z,
3131
norm=colors.SymLogNorm(linthresh=0.03, linscale=0.03,
32-
vmin=-1.0, vmax=1.0),
32+
vmin=-1.0, vmax=1.0, base=10),
3333
cmap='RdBu_r')
3434
fig.colorbar(pcm, ax=ax[0], extend='both')
3535

lib/matplotlib/colors.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ class SymLogNorm(Normalize):
12131213
*linthresh* allows the user to specify the size of this range
12141214
(-*linthresh*, *linthresh*).
12151215
"""
1216-
def __init__(self, linthresh, linscale=1.0,
1216+
def __init__(self, linthresh, linscale=1.0, base=None,
12171217
vmin=None, vmax=None, clip=False):
12181218
"""
12191219
Parameters
@@ -1224,14 +1224,29 @@ def __init__(self, linthresh, linscale=1.0,
12241224
linscale : float, default: 1
12251225
This allows the linear range (-*linthresh* to *linthresh*) to be
12261226
stretched relative to the logarithmic range. Its value is the
1227-
number of decades to use for each half of the linear range. For
1228-
example, when *linscale* == 1.0 (the default), the space used for
1229-
the positive and negative halves of the linear range will be equal
1230-
to one decade in the logarithmic range.
1227+
number of powers of *base* (decades for base 10) to use for each
1228+
half of the linear range. For example, when *linscale* == 1.0
1229+
(the default), the space used for the positive and negative halves
1230+
of the linear range will be equal to a decade in the logarithmic
1231+
range if ``base=10``.
1232+
base : float, default: None
1233+
For v3.2 the default is the old value of `np.e`, but that is
1234+
deprecated for v3.3 when base will default to 10. During the
1235+
transition, specify the *base* kwarg to avoid a deprecation
1236+
warning.
12311237
"""
12321238
Normalize.__init__(self, vmin, vmax, clip)
1239+
if base is None:
1240+
self._base = np.e
1241+
cbook.warn_deprecated("3.3", message="default base will change "
1242+
"from np.e to 10. To suppress this warning specify the base "
1243+
"kwarg.")
1244+
else:
1245+
self._base = base
1246+
self._log_base = np.log(self._base)
1247+
12331248
self.linthresh = float(linthresh)
1234-
self._linscale_adj = (linscale / (1.0 - np.e ** -1))
1249+
self._linscale_adj = (linscale / (1.0 - self._base ** -1))
12351250
if vmin is not None and vmax is not None:
12361251
self._transform_vmin_vmax()
12371252

@@ -1266,7 +1281,8 @@ def _transform(self, a):
12661281
with np.errstate(invalid="ignore"):
12671282
masked = np.abs(a) > self.linthresh
12681283
sign = np.sign(a[masked])
1269-
log = (self._linscale_adj + np.log(np.abs(a[masked]) / self.linthresh))
1284+
log = (self._linscale_adj +
1285+
np.log(np.abs(a[masked]) / self.linthresh) / self._log_base)
12701286
log *= sign * self.linthresh
12711287
a[masked] = log
12721288
a[~masked] *= self._linscale_adj
@@ -1276,7 +1292,8 @@ def _inv_transform(self, a):
12761292
"""Inverse inplace Transformation."""
12771293
masked = np.abs(a) > (self.linthresh * self._linscale_adj)
12781294
sign = np.sign(a[masked])
1279-
exp = np.exp(sign * a[masked] / self.linthresh - self._linscale_adj)
1295+
exp = np.power(self._base,
1296+
sign * a[masked] / self.linthresh - self._linscale_adj)
12801297
exp *= sign * self.linthresh
12811298
a[masked] = exp
12821299
a[~masked] /= self._linscale_adj

lib/matplotlib/tests/test_colors.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ def test_SymLogNorm():
398398
"""
399399
Test SymLogNorm behavior
400400
"""
401-
norm = mcolors.SymLogNorm(3, vmax=5, linscale=1.2)
401+
norm = mcolors.SymLogNorm(3, vmax=5, linscale=1.2, base=np.e)
402402
vals = np.array([-30, -1, 2, 6], dtype=float)
403403
normed_vals = norm(vals)
404404
expected = [0., 0.53980074, 0.826991, 1.02758204]
@@ -408,16 +408,30 @@ def test_SymLogNorm():
408408
_mask_tester(norm, vals)
409409

410410
# Ensure that specifying vmin returns the same result as above
411-
norm = mcolors.SymLogNorm(3, vmin=-30, vmax=5, linscale=1.2)
411+
norm = mcolors.SymLogNorm(3, vmin=-30, vmax=5, linscale=1.2, base=np.e)
412412
normed_vals = norm(vals)
413413
assert_array_almost_equal(normed_vals, expected)
414414

415+
# test something more easily checked.
416+
norm = mcolors.SymLogNorm(1, vmin=-np.e**3, vmax=np.e**3, base=np.e)
417+
nn = norm([-np.e**3, -np.e**2, -np.e**1, -1,
418+
0, 1, np.e**1, np.e**2, np.e**3])
419+
xx = np.array([0., 0.109123, 0.218246, 0.32737, 0.5, 0.67263,
420+
0.781754, 0.890877, 1.])
421+
assert_array_almost_equal(nn, xx)
422+
norm = mcolors.SymLogNorm(1, vmin=-10**3, vmax=10**3, base=10)
423+
nn = norm([-10**3, -10**2, -10**1, -1,
424+
0, 1, 10**1, 10**2, 10**3])
425+
xx = np.array([0., 0.121622, 0.243243, 0.364865, 0.5, 0.635135,
426+
0.756757, 0.878378, 1.])
427+
assert_array_almost_equal(nn, xx)
428+
415429

416430
def test_SymLogNorm_colorbar():
417431
"""
418432
Test un-called SymLogNorm in a colorbar.
419433
"""
420-
norm = mcolors.SymLogNorm(0.1, vmin=-1, vmax=1, linscale=1)
434+
norm = mcolors.SymLogNorm(0.1, vmin=-1, vmax=1, linscale=1, base=np.e)
421435
fig = plt.figure()
422436
mcolorbar.ColorbarBase(fig.add_subplot(111), norm=norm)
423437
plt.close(fig)
@@ -428,7 +442,7 @@ def test_SymLogNorm_single_zero():
428442
Test SymLogNorm to ensure it is not adding sub-ticks to zero label
429443
"""
430444
fig = plt.figure()
431-
norm = mcolors.SymLogNorm(1e-5, vmin=-1, vmax=1)
445+
norm = mcolors.SymLogNorm(1e-5, vmin=-1, vmax=1, base=np.e)
432446
cbar = mcolorbar.ColorbarBase(fig.add_subplot(111), norm=norm)
433447
ticks = cbar.get_ticks()
434448
assert sum(ticks == 0) == 1
@@ -905,9 +919,10 @@ def __add__(self, other):
905919
mydata = data.view(MyArray)
906920

907921
for norm in [mcolors.Normalize(), mcolors.LogNorm(),
908-
mcolors.SymLogNorm(3, vmax=5, linscale=1),
922+
mcolors.SymLogNorm(3, vmax=5, linscale=1, base=np.e),
909923
mcolors.Normalize(vmin=mydata.min(), vmax=mydata.max()),
910-
mcolors.SymLogNorm(3, vmin=mydata.min(), vmax=mydata.max()),
924+
mcolors.SymLogNorm(3, vmin=mydata.min(), vmax=mydata.max(),
925+
base=np.e),
911926
mcolors.PowerNorm(1)]:
912927
assert_array_equal(norm(mydata), norm(data))
913928
fig, ax = plt.subplots()

tutorials/colors/colormapnorms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898

9999
pcm = ax[0].pcolormesh(X, Y, Z,
100100
norm=colors.SymLogNorm(linthresh=0.03, linscale=0.03,
101-
vmin=-1.0, vmax=1.0),
101+
vmin=-1.0, vmax=1.0, base=10),
102102
cmap='RdBu_r')
103103
fig.colorbar(pcm, ax=ax[0], extend='both')
104104

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy