|
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. |
20 | 25 |
|
21 | | -Note that colorbar labels do not come out looking very good. |
22 | | -""" |
| 26 | +def rbf(x, y): |
| 27 | + return 1.0 / (1 + 5 * ((x ** 2) + (y ** 2))) |
23 | 28 |
|
24 | | -N = 100 |
| 29 | +N = 200 |
| 30 | +gain = 8 |
25 | 31 | 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 = (Z1 - Z2) * 2 |
| 32 | +Z1 = rbf(X + 0.5, Y + 0.5) |
| 33 | +Z2 = rbf(X - 0.5, Y - 0.5) |
| 34 | +Z = gain * Z1 - Z2 |
| 35 | + |
| 36 | +shadeopts = {'cmap': 'PRGn', 'shading': 'gouraud'} |
| 37 | +colormap = 'PRGn' |
| 38 | +lnrwidth = 0.5 |
29 | 39 |
|
30 | | -fig, ax = plt.subplots(2, 1) |
| 40 | +fig, ax = plt.subplots(2, 1, sharex=True, sharey=True) |
31 | 41 |
|
32 | 42 | pcm = ax[0].pcolormesh(X, Y, Z, |
33 | | - norm=colors.SymLogNorm(linthresh=0.03, linscale=0.03, |
34 | | - vmin=-1.0, vmax=1.0, base=10), |
35 | | - cmap='RdBu_r', shading='nearest') |
| 43 | + norm=colors.SymLogNorm(linthresh=lnrwidth, linscale=1, |
| 44 | + vmin=-gain, vmax=gain, base=10), |
| 45 | + **shadeopts) |
36 | 46 | fig.colorbar(pcm, ax=ax[0], extend='both') |
| 47 | +ax[0].text(-2.5, 1.5, 'symlog') |
37 | 48 |
|
38 | | -pcm = ax[1].pcolormesh(X, Y, Z, cmap='RdBu_r', vmin=-np.max(Z), |
39 | | - shading='nearest') |
| 49 | +pcm = ax[1].pcolormesh(X, Y, Z, vmin=-gain, vmax=gain, |
| 50 | + **shadeopts) |
40 | 51 | fig.colorbar(pcm, ax=ax[1], extend='both') |
| 52 | +ax[1].text(-2.5, 1.5, 'linear') |
| 53 | + |
| 54 | + |
| 55 | +############################################################################### |
| 56 | +# In order to find the best visualization for any particular dataset, |
| 57 | +# it may be necessary to experiment with multiple different color scales. |
| 58 | +# As well as the `~.colors.SymLogNorm` scaling, there is also |
| 59 | +# the option of using `~.colors.AsinhNorm` (experimental), which has a smoother |
| 60 | +# transition between the linear and logarithmic regions of the transformation |
| 61 | +# applied to the data values, "Z". |
| 62 | +# In the plots below, it may be possible to see contour-like artifacts |
| 63 | +# around each hump despite there being no sharp features |
| 64 | +# in the dataset itself. The ``asinh`` scaling shows a smoother shading |
| 65 | +# of each hump. |
| 66 | + |
| 67 | +fig, ax = plt.subplots(2, 1, sharex=True, sharey=True) |
| 68 | + |
| 69 | +pcm = ax[0].pcolormesh(X, Y, Z, |
| 70 | + norm=colors.SymLogNorm(linthresh=lnrwidth, linscale=1, |
| 71 | + vmin=-gain, vmax=gain, base=10), |
| 72 | + **shadeopts) |
| 73 | +fig.colorbar(pcm, ax=ax[0], extend='both') |
| 74 | +ax[0].text(-2.5, 1.5, 'symlog') |
| 75 | + |
| 76 | +pcm = ax[1].pcolormesh(X, Y, Z, |
| 77 | + norm=colors.AsinhNorm(linear_width=lnrwidth, |
| 78 | + vmin=-gain, vmax=gain), |
| 79 | + **shadeopts) |
| 80 | +fig.colorbar(pcm, ax=ax[1], extend='both') |
| 81 | +ax[1].text(-2.5, 1.5, 'asinh') |
| 82 | + |
41 | 83 |
|
42 | 84 | plt.show() |
0 commit comments