Skip to content

Commit b01d03c

Browse files
committed
Reworked SymLogNorm demonstration, and added comparision with AsinhNorm
1 parent ae57d8d commit b01d03c

File tree

1 file changed

+59
-19
lines changed

1 file changed

+59
-19
lines changed
Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,82 @@
11
"""
22
==================================
3-
Colormap Normalizations Symlognorm
3+
Colormap Normalizations SymLogNorm
44
==================================
55
66
Demonstration of using norm to map colormaps onto data in non-linear ways.
77
88
.. redirect-from:: /gallery/userdemo/colormap_normalization_symlognorm
99
"""
1010

11+
########################################
12+
# Synthetic dataset consisting of two humps, one negative and one positive,
13+
# the positive with 8-times the amplitude.
14+
# Linearly, the negative hump is almost invisible,
15+
# and it is very difficult to see any detail of its profile.
16+
# With the logarithmic scaling applied to both positive and negative values,
17+
# it is much easier to see the shape of each hump.
18+
#
19+
# See `~.colors.SymLogNorm`.
20+
1121
import numpy as np
1222
import matplotlib.pyplot as plt
1323
import matplotlib.colors as colors
1424

15-
"""
16-
SymLogNorm: two humps, one negative and one positive, The positive
17-
with 5-times the amplitude. Linearly, you cannot see detail in the
18-
negative hump. Here we logarithmically scale the positive and
19-
negative data separately.
25+
N = 200
26+
gain = 8
27+
X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]
28+
rbf = lambda x, y: 1.0 / (1 + 5 * ((x ** 2) + (y ** 2)))
29+
Z1 = rbf(X + 0.5, Y + 0.5)
30+
Z2 = rbf(X - 0.5, Y - 0.5)
31+
Z = gain * Z1 - Z2
2032

21-
Note that colorbar labels do not come out looking very good.
22-
"""
33+
shadeopts = { 'cmap': 'PRGn', 'shading': 'gouraud' }
34+
colormap = 'PRGn'
35+
lnrwidth = 0.2
2336

24-
N = 100
25-
X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]
26-
Z1 = np.exp(-X**2 - Y**2)
27-
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
28-
Z = 5 * Z1 - Z2
37+
fig, ax = plt.subplots(2, 1, sharex=True, sharey=True)
2938

30-
fig, ax = plt.subplots(2, 1)
39+
pcm = ax[0].pcolormesh(X, Y, Z,
40+
norm=colors.SymLogNorm(linthresh=lnrwidth, linscale=1,
41+
vmin=-gain, vmax=gain, base=10),
42+
**shadeopts)
43+
fig.colorbar(pcm, ax=ax[0], extend='both')
44+
ax[0].text(-2.5, 1.5, 'symlog')
45+
46+
pcm = ax[1].pcolormesh(X, Y, Z, vmin=-gain, vmax=gain,
47+
**shadeopts)
48+
fig.colorbar(pcm, ax=ax[1], extend='both')
49+
ax[1].text(-2.5, 1.5, 'linear')
50+
51+
52+
########################################
53+
# Clearly, tt may be necessary to experiment with multiple different
54+
# colorscales in order to find the best visualization for
55+
# any particular dataset.
56+
# As well as the `~.colors.SymLogNorm` scaling, there is also
57+
# the option of using the `~.colors.AsinhNorm`, which has a smoother
58+
# transition between the linear and logarithmic regions of the transformation
59+
# applied to the "z" axis.
60+
# In the plots below, it may be possible to see ring-like artifacts
61+
# in the lower-amplitude, negative, hump shown in purple despite
62+
# there being no sharp features in the dataset itself.
63+
# The ``asinh`` scaling shows a smoother shading of each hump.
64+
65+
fig, ax = plt.subplots(2, 1, sharex=True, sharey=True)
3166

3267
pcm = ax[0].pcolormesh(X, Y, Z,
33-
norm=colors.SymLogNorm(linthresh=0.5, linscale=1,
34-
vmin=-5, vmax=5, base=10),
35-
cmap='RdBu_r', shading='nearest')
68+
norm=colors.SymLogNorm(linthresh=lnrwidth, linscale=1,
69+
vmin=-gain, vmax=gain, base=10),
70+
**shadeopts)
3671
fig.colorbar(pcm, ax=ax[0], extend='both')
72+
ax[0].text(-2.5, 1.5, 'symlog')
3773

38-
pcm = ax[1].pcolormesh(X, Y, Z, cmap='RdBu_r', vmin=-5, vmax=5,
39-
shading='nearest')
74+
pcm = ax[1].pcolormesh(X, Y, Z,
75+
norm=colors.AsinhNorm(linear_width=lnrwidth,
76+
vmin=-gain, vmax=gain),
77+
**shadeopts)
4078
fig.colorbar(pcm, ax=ax[1], extend='both')
79+
ax[1].text(-2.5, 1.5, 'asinh')
80+
4181

4282
plt.show()

0 commit comments

Comments
 (0)