Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3a8e75e

Browse filesBrowse files
authored
Merge pull request #16404 from jklymak/fix-add-base-symlognorm
FIX: add base kwarg to symlognor
2 parents 8451629 + fe5fdf8 commit 3a8e75e
Copy full SHA for 3a8e75e

File tree

Expand file treeCollapse file tree

6 files changed

+58
-18
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+58
-18
lines changed

‎doc/api/next_api_changes/behaviour.rst

Copy file name to clipboardExpand all lines: doc/api/next_api_changes/behaviour.rst
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,11 @@ Previously, rcParams entries whose values were color-like accepted "spurious"
8585
extra letters or characters in the "middle" of the string, e.g. ``"(0, 1a, '0.5')"``
8686
would be interpreted as ``(0, 1, 0.5)``. These extra characters (including the
8787
internal quotes) now cause a ValueError to be raised.
88+
89+
`.SymLogNorm` now has a *base* parameter
90+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
91+
92+
Previously, `.SymLogNorm` had no *base* kwarg, and defaulted to ``base=np.e``
93+
whereas the documentation said it was ``base=10``. In preparation to make
94+
the default 10, calling `.SymLogNorm` without the new *base* kwarg emits a
95+
deprecation warning.

‎examples/userdemo/colormap_normalizations.py

Copy file name to clipboardExpand all lines: examples/userdemo/colormap_normalizations.py
+1-1Lines 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

Copy file name to clipboardExpand all lines: examples/userdemo/colormap_normalizations_symlognorm.py
+1-1Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/colors.py
+26-9Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,8 +1202,8 @@ class SymLogNorm(Normalize):
12021202
*linthresh* allows the user to specify the size of this range
12031203
(-*linthresh*, *linthresh*).
12041204
"""
1205-
def __init__(self, linthresh, linscale=1.0,
1206-
vmin=None, vmax=None, clip=False):
1205+
def __init__(self, linthresh, linscale=1.0, vmin=None, vmax=None,
1206+
clip=False, *, base=None):
12071207
"""
12081208
Parameters
12091209
----------
@@ -1213,14 +1213,29 @@ def __init__(self, linthresh, linscale=1.0,
12131213
linscale : float, default: 1
12141214
This allows the linear range (-*linthresh* to *linthresh*) to be
12151215
stretched relative to the logarithmic range. Its value is the
1216-
number of decades to use for each half of the linear range. For
1217-
example, when *linscale* == 1.0 (the default), the space used for
1218-
the positive and negative halves of the linear range will be equal
1219-
to one decade in the logarithmic range.
1216+
number of powers of *base* (decades for base 10) to use for each
1217+
half of the linear range. For example, when *linscale* == 1.0
1218+
(the default), the space used for the positive and negative halves
1219+
of the linear range will be equal to a decade in the logarithmic
1220+
range if ``base=10``.
1221+
base : float, default: None
1222+
For v3.2 the default is the old value of ``np.e``, but that is
1223+
deprecated for v3.3 when base will default to 10. During the
1224+
transition, specify the *base* kwarg to avoid a deprecation
1225+
warning.
12201226
"""
12211227
Normalize.__init__(self, vmin, vmax, clip)
1228+
if base is None:
1229+
self._base = np.e
1230+
cbook.warn_deprecated("3.3", message="default base will change "
1231+
"from np.e to 10. To suppress this warning specify the base "
1232+
"kwarg.")
1233+
else:
1234+
self._base = base
1235+
self._log_base = np.log(self._base)
1236+
12221237
self.linthresh = float(linthresh)
1223-
self._linscale_adj = (linscale / (1.0 - np.e ** -1))
1238+
self._linscale_adj = (linscale / (1.0 - self._base ** -1))
12241239
if vmin is not None and vmax is not None:
12251240
self._transform_vmin_vmax()
12261241

@@ -1255,7 +1270,8 @@ def _transform(self, a):
12551270
with np.errstate(invalid="ignore"):
12561271
masked = np.abs(a) > self.linthresh
12571272
sign = np.sign(a[masked])
1258-
log = (self._linscale_adj + np.log(np.abs(a[masked]) / self.linthresh))
1273+
log = (self._linscale_adj +
1274+
np.log(np.abs(a[masked]) / self.linthresh) / self._log_base)
12591275
log *= sign * self.linthresh
12601276
a[masked] = log
12611277
a[~masked] *= self._linscale_adj
@@ -1265,7 +1281,8 @@ def _inv_transform(self, a):
12651281
"""Inverse inplace Transformation."""
12661282
masked = np.abs(a) > (self.linthresh * self._linscale_adj)
12671283
sign = np.sign(a[masked])
1268-
exp = np.exp(sign * a[masked] / self.linthresh - self._linscale_adj)
1284+
exp = np.power(self._base,
1285+
sign * a[masked] / self.linthresh - self._linscale_adj)
12691286
exp *= sign * self.linthresh
12701287
a[masked] = exp
12711288
a[~masked] /= self._linscale_adj

‎lib/matplotlib/tests/test_colors.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_colors.py
+21-6Lines 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

Copy file name to clipboardExpand all lines: tutorials/colors/colormapnorms.py
+1-1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.