|
| 1 | +""" |
| 2 | +Demonstration of using norm to map colormaps onto data in non-linear ways. |
| 3 | +""" |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import matplotlib.pyplot as plt |
| 7 | +import matplotlib.colors as colors |
| 8 | +from matplotlib.mlab import bivariate_normal |
| 9 | + |
| 10 | +''' |
| 11 | +Lognorm: Instead of pcolor log10(Z1) you can have colorbars that have |
| 12 | +the exponential labels using a norm. |
| 13 | +''' |
| 14 | +N = 100 |
| 15 | +X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)] |
| 16 | + |
| 17 | +# A low hump with a spike coming out of the top right. Needs to have |
| 18 | +# z/colour axis on a log scale so we see both hump and spike. linear |
| 19 | +# scale only shows the spike. |
| 20 | +Z1 = bivariate_normal(X, Y, 0.1, 0.2, 1.0, 1.0) + \ |
| 21 | + 0.1 * bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) |
| 22 | + |
| 23 | +fig, ax = plt.subplots(2, 1) |
| 24 | + |
| 25 | +pcm = ax[0].pcolor(X, Y, Z1, |
| 26 | + norm=colors.LogNorm(vmin=Z1.min(), vmax=Z1.max()), |
| 27 | + cmap='PuBu_r') |
| 28 | +fig.colorbar(pcm, ax=ax[0], extend='max') |
| 29 | + |
| 30 | +pcm = ax[1].pcolor(X, Y, Z1, cmap='PuBu_r') |
| 31 | +fig.colorbar(pcm, ax=ax[1], extend='max') |
| 32 | +fig.show() |
| 33 | + |
| 34 | + |
| 35 | +''' |
| 36 | +PowerNorm: Here a power-law trend in X partially obscures a rectified |
| 37 | +sine wave in Y. We can remove the power law using a PowerNorm. |
| 38 | +''' |
| 39 | +X, Y = np.mgrid[0:3:complex(0, N), 0:2:complex(0, N)] |
| 40 | +Z1 = (1 + np.sin(Y * 10.)) * X**(2.) |
| 41 | + |
| 42 | +fig, ax = plt.subplots(2, 1) |
| 43 | + |
| 44 | +pcm = ax[0].pcolormesh(X, Y, Z1, norm=colors.PowerNorm(gamma=1./2.), |
| 45 | + cmap='PuBu_r') |
| 46 | +fig.colorbar(pcm, ax=ax[0], extend='max') |
| 47 | + |
| 48 | +pcm = ax[1].pcolormesh(X, Y, Z1, cmap='PuBu_r') |
| 49 | +fig.colorbar(pcm, ax=ax[1], extend='max') |
| 50 | +fig.show() |
| 51 | + |
| 52 | +''' |
| 53 | +SymLogNorm: two humps, one negative and one positive, The positive |
| 54 | +with 5-times the amplitude. Linearly, you cannot see detail in the |
| 55 | +negative hump. Here we logarithmically scale the positive and |
| 56 | +negative data separately. |
| 57 | +
|
| 58 | +Note that colorbar labels do not come out looking very good. |
| 59 | +''' |
| 60 | + |
| 61 | +X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)] |
| 62 | +Z1 = (bivariate_normal(X, Y, 1., 1., 1.0, 1.0))**2 \ |
| 63 | + - 0.4 * (bivariate_normal(X, Y, 1.0, 1.0, -1.0, 0.0))**2 |
| 64 | +Z1 = Z1/0.03 |
| 65 | + |
| 66 | +fig, ax = plt.subplots(2, 1) |
| 67 | + |
| 68 | +pcm = ax[0].pcolormesh(X, Y, Z1, |
| 69 | + norm=colors.SymLogNorm(linthresh=0.03, linscale=0.03, |
| 70 | + vmin=-1.0, vmax=1.0), |
| 71 | + cmap='RdBu_r') |
| 72 | +fig.colorbar(pcm, ax=ax[0], extend='both') |
| 73 | + |
| 74 | +pcm = ax[1].pcolormesh(X, Y, Z1, cmap='RdBu_r', vmin=-np.max(Z1)) |
| 75 | +fig.colorbar(pcm, ax=ax[1], extend='both') |
| 76 | +fig.show() |
| 77 | + |
| 78 | + |
| 79 | +''' |
| 80 | +Custom Norm: An example with a customized normalization. This one |
| 81 | +uses the example above, and normalizes the negative data differently |
| 82 | +from the positive. |
| 83 | +''' |
| 84 | +X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)] |
| 85 | +Z1 = (bivariate_normal(X, Y, 1., 1., 1.0, 1.0))**2 \ |
| 86 | + - 0.4 * (bivariate_normal(X, Y, 1.0, 1.0, -1.0, 0.0))**2 |
| 87 | +Z1 = Z1/0.03 |
| 88 | + |
| 89 | +# Example of making your own norm. Also see matplotlib.colors. |
| 90 | +# From Joe Kington: This one gives two different linear ramps: |
| 91 | + |
| 92 | + |
| 93 | +class MidpointNormalize(colors.Normalize): |
| 94 | + def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False): |
| 95 | + self.midpoint = midpoint |
| 96 | + colors.Normalize.__init__(self, vmin, vmax, clip) |
| 97 | + |
| 98 | + def __call__(self, value, clip=None): |
| 99 | + # I'm ignoring masked values and all kinds of edge cases to make a |
| 100 | + # simple example... |
| 101 | + x, y = [self.vmin, self.midpoint, self.vmax], [0, 0.5, 1] |
| 102 | + return np.ma.masked_array(np.interp(value, x, y)) |
| 103 | +##### |
| 104 | +fig, ax = plt.subplots(2, 1) |
| 105 | + |
| 106 | +pcm = ax[0].pcolormesh(X, Y, Z1, |
| 107 | + norm=MidpointNormalize(midpoint=0.), |
| 108 | + cmap='RdBu_r') |
| 109 | +fig.colorbar(pcm, ax=ax[0], extend='both') |
| 110 | + |
| 111 | +pcm = ax[1].pcolormesh(X, Y, Z1, cmap='RdBu_r', vmin=-np.max(Z1)) |
| 112 | +fig.colorbar(pcm, ax=ax[1], extend='both') |
| 113 | +fig.show() |
| 114 | + |
| 115 | +''' |
| 116 | +BoundaryNorm: For this one you provide the boundaries for your colors, |
| 117 | +and the Norm puts the first color in between the first pair, the |
| 118 | +second color between the second pair, etc. |
| 119 | +''' |
| 120 | + |
| 121 | +fig, ax = plt.subplots(3, 1, figsize=(8, 8)) |
| 122 | +ax = ax.flatten() |
| 123 | +# even bounds gives a contour-like effect |
| 124 | +bounds = np.linspace(-1, 1, 10) |
| 125 | +norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256) |
| 126 | +pcm = ax[0].pcolormesh(X, Y, Z1, |
| 127 | + norm=norm, |
| 128 | + cmap='RdBu_r') |
| 129 | +fig.colorbar(pcm, ax=ax[0], extend='both', orientation='vertical') |
| 130 | + |
| 131 | +# uneven bounds changes the colormapping: |
| 132 | +bounds = np.array([-0.25, -0.125, 0, 0.5, 1]) |
| 133 | +norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256) |
| 134 | +pcm = ax[1].pcolormesh(X, Y, Z1, norm=norm, cmap='RdBu_r') |
| 135 | +fig.colorbar(pcm, ax=ax[1], extend='both', orientation='vertical') |
| 136 | + |
| 137 | +pcm = ax[2].pcolormesh(X, Y, Z1, cmap='RdBu_r', vmin=-np.max(Z1)) |
| 138 | +fig.colorbar(pcm, ax=ax[2], extend='both', orientation='vertical') |
| 139 | +fig.show() |
| 140 | + |
0 commit comments