|
1 | 1 | """ |
2 | 2 | ================================== |
3 | | -Colormap Normalizations Symlognorm |
| 3 | +Colormap Normalizations SymLogNorm |
4 | 4 | ================================== |
5 | 5 |
|
6 | 6 | Demonstration of using norm to map colormaps onto data in non-linear ways. |
7 | 7 |
|
8 | 8 | .. redirect-from:: /gallery/userdemo/colormap_normalization_symlognorm |
9 | 9 | """ |
10 | 10 |
|
| 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 | + |
11 | 21 | import numpy as np |
12 | 22 | import matplotlib.pyplot as plt |
13 | 23 | import matplotlib.colors as colors |
14 | 24 |
|
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 |
20 | 32 |
|
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 |
23 | 36 |
|
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) |
29 | 38 |
|
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) |
31 | 66 |
|
32 | 67 | 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) |
36 | 71 | fig.colorbar(pcm, ax=ax[0], extend='both') |
| 72 | +ax[0].text(-2.5, 1.5, 'symlog') |
37 | 73 |
|
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) |
40 | 78 | fig.colorbar(pcm, ax=ax[1], extend='both') |
| 79 | +ax[1].text(-2.5, 1.5, 'asinh') |
| 80 | + |
41 | 81 |
|
42 | 82 | plt.show() |
0 commit comments