|
| 1 | +""" |
| 2 | +============================================ |
| 3 | +Examples of arbitrary colormap normalization |
| 4 | +============================================ |
| 5 | +
|
| 6 | +Here I plot an image array with data spanning for a large dynamic range, |
| 7 | +using different normalizations. Look at how each of them enhances |
| 8 | +different features. |
| 9 | +
|
| 10 | +""" |
| 11 | + |
| 12 | +import ArbitraryNorm as colors |
| 13 | + |
| 14 | +import numpy as np |
| 15 | +# import matplotlib.colors as colors |
| 16 | +import matplotlib.pyplot as plt |
| 17 | +import matplotlib.cm as cm |
| 18 | + |
| 19 | +# Creating some toy data |
| 20 | +xmax = 16 * np.pi |
| 21 | +x = np.linspace(0, xmax, 1024) |
| 22 | +y = np.linspace(-2, 2, 512) |
| 23 | +X, Y = np.meshgrid(x, y) |
| 24 | + |
| 25 | +data = np.zeros(X.shape) |
| 26 | + |
| 27 | + |
| 28 | +def gauss2d(x, y, a0, x0, y0, wx, wy): |
| 29 | + return a0 * np.exp(-(x - x0)**2 / wx**2 - (y - y0)**2 / wy**2) |
| 30 | + |
| 31 | +maskY = (Y > -1) * (Y <= 0) |
| 32 | +N = 31 |
| 33 | +for i in range(N): |
| 34 | + maskX = (X > (i * (xmax / N))) * (X <= ((i + 1) * (xmax / N))) |
| 35 | + mask = maskX * maskY |
| 36 | + data[mask] += gauss2d(X[mask], Y[mask], 2. * i / (N - 1), (i + 0.5) * |
| 37 | + (xmax / N), -0.25, xmax / (3 * N), 0.07) |
| 38 | + data[mask] -= gauss2d(X[mask], Y[mask], 1. * i / (N - 1), (i + 0.5) * |
| 39 | + (xmax / N), -0.75, xmax / (3 * N), 0.07) |
| 40 | + |
| 41 | +maskY = (Y > 0) * (Y <= 1) |
| 42 | +data[maskY] = np.cos(X[maskY]) * Y[maskY]**2 |
| 43 | + |
| 44 | +N = 61 |
| 45 | +maskY = (Y > 1) * (Y <= 2.) |
| 46 | +for i, val in enumerate(np.linspace(-1, 1, N)): |
| 47 | + if val < 0: |
| 48 | + aux = val |
| 49 | + if val > 0: |
| 50 | + aux = val * 2 |
| 51 | + |
| 52 | + maskX = (X > (i * (xmax / N))) * (X <= ((i + 1) * (xmax / N))) |
| 53 | + data[maskX * maskY] = aux |
| 54 | + |
| 55 | +N = 11 |
| 56 | +maskY = (Y <= -1) |
| 57 | +for i, val in enumerate(np.linspace(-1, 1, N)): |
| 58 | + if val < 0: |
| 59 | + factor = 1 |
| 60 | + if val >= 0: |
| 61 | + factor = 2 |
| 62 | + maskX = (X > (i * (xmax / N))) * (X <= ((i + 1) * (xmax / N))) |
| 63 | + mask = maskX * maskY |
| 64 | + data[mask] = val * factor |
| 65 | + |
| 66 | + if i != N - 1: |
| 67 | + data[mask] += gauss2d(X[mask], Y[mask], 0.03 * factor, (i + 0.5) * |
| 68 | + (xmax / N), -1.25, xmax / (3 * N), 0.07) |
| 69 | + if i != 0: |
| 70 | + data[mask] -= gauss2d(X[mask], Y[mask], 0.1 * factor, (i + 0.5) * |
| 71 | + (xmax / N), -1.75, xmax / (3 * N), 0.07) |
| 72 | + |
| 73 | + |
| 74 | +cmap = cm.spectral |
| 75 | + |
| 76 | + |
| 77 | +def makePlot(norm, label=''): |
| 78 | + fig, ax = plt.subplots() |
| 79 | + cax = ax.pcolormesh(x, y, data, cmap=cmap, norm=norm) |
| 80 | + ax.set_title(label) |
| 81 | + ax.set_xlim(0, xmax) |
| 82 | + ax.set_ylim(-2, 2) |
| 83 | + if norm: |
| 84 | + ticks = norm.ticks() |
| 85 | + else: |
| 86 | + ticks = None |
| 87 | + cbar = fig.colorbar(cax, format='%.3g', ticks=ticks) |
| 88 | + |
| 89 | + |
| 90 | +makePlot(None, 'Regular linear scale') |
| 91 | + |
| 92 | +# Example of logarithm normalization using FuncNorm |
| 93 | +norm = colors.FuncNorm(f=lambda x: np.log10(x), |
| 94 | + finv=lambda x: 10.**(x), vmin=0.01, vmax=2) |
| 95 | +makePlot(norm, "Log normalization using FuncNorm") |
| 96 | +# The same can be achived with |
| 97 | +# norm = colors.FuncNorm(f='log',vmin=0.01,vmax=2) |
| 98 | + |
| 99 | +# Example of root normalization using FuncNorm |
| 100 | +norm = colors.FuncNorm(f='sqrt', vmin=0.0, vmax=2) |
| 101 | +makePlot(norm, "Root normalization using FuncNorm") |
| 102 | + |
| 103 | +# Performing a symmetric amplification of the features around 0 |
| 104 | +norm = colors.MirrorPiecewiseNorm(fpos='crt') |
| 105 | +makePlot(norm, "Amplified features symetrically around \n" |
| 106 | + "0 with MirrorPiecewiseNorm") |
| 107 | + |
| 108 | + |
| 109 | +# Amplifying features near 0.6 with MirrorPiecewiseNorm |
| 110 | +norm = colors.MirrorPiecewiseNorm(fpos='crt', fneg='crt', |
| 111 | + center_cm=0.35, |
| 112 | + center_data=0.6) |
| 113 | +makePlot(norm, "Amplifying positive and negative features\n" |
| 114 | + "standing on 0.6 with MirrorPiecewiseNorm") |
| 115 | + |
| 116 | +# Amplifying features near both -0.4 and near 1.2 with PiecewiseNorm |
| 117 | +norm = colors.PiecewiseNorm(flist=['cubic', 'crt', 'cubic', 'crt'], |
| 118 | + refpoints_cm=[0.25, 0.5, 0.75], |
| 119 | + refpoints_data=[-0.4, 1, 1.2]) |
| 120 | +makePlot(norm, "Amplifying positive and negative features standing\n" |
| 121 | + " on -0.4 and on 1.2 with PiecewiseNorm") |
| 122 | + |
| 123 | +# Amplifying features near both -0.4 and near 1.2 with PiecewiseNorm |
| 124 | +norm = colors.PiecewiseNorm(flist=['linear', 'crt', 'crt'], |
| 125 | + refpoints_cm=[0.2, 0.6], |
| 126 | + refpoints_data=[-0.6, 1.2]) |
| 127 | +makePlot(norm, "Amplifying only positive features standing on -0.6\n" |
| 128 | + " and on 1.2 with PiecewiseNorm") |
| 129 | + |
| 130 | + |
| 131 | +plt.show() |
0 commit comments