|
47 | 47 | import matplotlib.pyplot as plt |
48 | 48 | import matplotlib.colors as colors |
49 | 49 | import matplotlib.cbook as cbook |
| 50 | +from matplotlib import cm |
50 | 51 |
|
51 | 52 | N = 100 |
52 | 53 | X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)] |
|
69 | 70 | fig.colorbar(pcm, ax=ax[1], extend='max') |
70 | 71 | plt.show() |
71 | 72 |
|
| 73 | +############################################################################### |
| 74 | +# Centered |
| 75 | +# -------- |
| 76 | +# |
| 77 | +# In many cases, data is symmetrical around a center, for example, positive and |
| 78 | +# negative anomalies around a center 0. In this case, we would like the center |
| 79 | +# to be mapped to 0.5 and the datapoint with the largest deviation from the |
| 80 | +# center to be mapped to 1.0, if its value is greater than the center, or -1.0 |
| 81 | +# otherwise. The norm `.colors.CenteredNorm` creates such a mapping |
| 82 | +# automatically. It is well suited to be combined with a divergent colormap |
| 83 | +# which uses different colors edges that meet in the center at an unsaturated |
| 84 | +# color. |
| 85 | +# |
| 86 | +# If the center of symmetry is different from 0, it can be set with the |
| 87 | +# *vcenter* argument. For logarithmic scaling on both sides of the center, see |
| 88 | +# `.colors.SymLogNorm` below; to apply a different mapping above and below the |
| 89 | +# center, use `.colors.TwoSlopeNorm` below. |
| 90 | + |
| 91 | +delta = 0.1 |
| 92 | +x = np.arange(-3.0, 4.001, delta) |
| 93 | +y = np.arange(-4.0, 3.001, delta) |
| 94 | +X, Y = np.meshgrid(x, y) |
| 95 | +Z1 = np.exp(-X**2 - Y**2) |
| 96 | +Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2) |
| 97 | +Z = (0.9*Z1 - 0.5*Z2) * 2 |
| 98 | + |
| 99 | +# select a divergent colormap |
| 100 | +cmap = cm.coolwarm |
| 101 | + |
| 102 | +fig, (ax1, ax2) = plt.subplots(ncols=2) |
| 103 | +pc = ax1.pcolormesh(Z, cmap=cmap) |
| 104 | +fig.colorbar(pc, ax=ax1) |
| 105 | +ax1.set_title('Normalize()') |
| 106 | + |
| 107 | +pc = ax2.pcolormesh(Z, norm=colors.CenteredNorm(), cmap=cmap) |
| 108 | +fig.colorbar(pc, ax=ax2) |
| 109 | +ax2.set_title('CenteredNorm()') |
| 110 | + |
| 111 | +plt.show() |
| 112 | + |
72 | 113 | ############################################################################### |
73 | 114 | # Symmetric logarithmic |
74 | 115 | # --------------------- |
|
0 commit comments