|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import matplotlib.pyplot as plt |
| 5 | +from matplotlib.colors import LinearSegmentedColormap |
| 6 | + |
| 7 | +""" |
| 8 | +
|
| 9 | +Example: suppose you want red to increase from 0 to 1 over the bottom |
| 10 | +half, green to do the same over the middle half, and blue over the top |
| 11 | +half. Then you would use: |
| 12 | +
|
| 13 | +cdict = {'red': ((0.0, 0.0, 0.0), |
| 14 | + (0.5, 1.0, 1.0), |
| 15 | + (1.0, 1.0, 1.0)), |
| 16 | +
|
| 17 | + 'green': ((0.0, 0.0, 0.0), |
| 18 | + (0.25, 0.0, 0.0), |
| 19 | + (0.75, 1.0, 1.0), |
| 20 | + (1.0, 1.0, 1.0)), |
| 21 | +
|
| 22 | + 'blue': ((0.0, 0.0, 0.0), |
| 23 | + (0.5, 0.0, 0.0), |
| 24 | + (1.0, 1.0, 1.0))} |
| 25 | +
|
| 26 | +If, as in this example, there are no discontinuities in the r, g, and b |
| 27 | +components, then it is quite simple: the second and third element of |
| 28 | +each tuple, above, is the same--call it "y". The first element ("x") |
| 29 | +defines interpolation intervals over the full range of 0 to 1, and it |
| 30 | +must span that whole range. In other words, the values of x divide the |
| 31 | +0-to-1 range into a set of segments, and y gives the end-point color |
| 32 | +values for each segment. |
| 33 | +
|
| 34 | +Now consider the green. cdict['green'] is saying that for |
| 35 | +0 <= x <= 0.25, y is zero; no green. |
| 36 | +0.25 < x <= 0.75, y varies linearly from 0 to 1. |
| 37 | +x > 0.75, y remains at 1, full green. |
| 38 | +
|
| 39 | +If there are discontinuities, then it is a little more complicated. |
| 40 | +Label the 3 elements in each row in the cdict entry for a given color as |
| 41 | +(x, y0, y1). Then for values of x between x[i] and x[i+1] the color |
| 42 | +value is interpolated between y1[i] and y0[i+1]. |
| 43 | +
|
| 44 | +Going back to the cookbook example, look at cdict['red']; because y0 != |
| 45 | +y1, it is saying that for x from 0 to 0.5, red increases from 0 to 1, |
| 46 | +but then it jumps down, so that for x from 0.5 to 1, red increases from |
| 47 | +0.7 to 1. Green ramps from 0 to 1 as x goes from 0 to 0.5, then jumps |
| 48 | +back to 0, and ramps back to 1 as x goes from 0.5 to 1. |
| 49 | +
|
| 50 | +row i: x y0 y1 |
| 51 | + / |
| 52 | + / |
| 53 | +row i+1: x y0 y1 |
| 54 | +
|
| 55 | +Above is an attempt to show that for x in the range x[i] to x[i+1], the |
| 56 | +interpolation is between y1[i] and y0[i+1]. So, y0[0] and y1[-1] are |
| 57 | +never used. |
| 58 | +
|
| 59 | +""" |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +cdict1 = {'red': ((0.0, 0.0, 0.0), |
| 64 | + (0.5, 0.0, 0.1), |
| 65 | + (1.0, 1.0, 1.0)), |
| 66 | + |
| 67 | + 'green': ((0.0, 0.0, 0.0), |
| 68 | + (1.0, 0.0, 0.0)), |
| 69 | + |
| 70 | + 'blue': ((0.0, 0.0, 1.0), |
| 71 | + (0.5, 0.1, 0.0), |
| 72 | + (1.0, 0.0, 0.0)) |
| 73 | + } |
| 74 | + |
| 75 | +cdict2 = {'red': ((0.0, 0.0, 0.0), |
| 76 | + (0.5, 0.0, 1.0), |
| 77 | + (1.0, 0.1, 1.0)), |
| 78 | + |
| 79 | + 'green': ((0.0, 0.0, 0.0), |
| 80 | + (1.0, 0.0, 0.0)), |
| 81 | + |
| 82 | + 'blue': ((0.0, 0.0, 0.1), |
| 83 | + (0.5, 1.0, 0.0), |
| 84 | + (1.0, 0.0, 0.0)) |
| 85 | + } |
| 86 | + |
| 87 | +cdict3 = {'red': ((0.0, 0.0, 0.0), |
| 88 | + (0.25,0.0, 0.0), |
| 89 | + (0.5, 0.8, 1.0), |
| 90 | + (0.75,1.0, 1.0), |
| 91 | + (1.0, 0.4, 1.0)), |
| 92 | + |
| 93 | + 'green': ((0.0, 0.0, 0.0), |
| 94 | + (0.25,0.0, 0.0), |
| 95 | + (0.5, 0.9, 0.9), |
| 96 | + (0.75,0.0, 0.0), |
| 97 | + (1.0, 0.0, 0.0)), |
| 98 | + |
| 99 | + 'blue': ((0.0, 0.0, 0.4), |
| 100 | + (0.25,1.0, 1.0), |
| 101 | + (0.5, 1.0, 0.8), |
| 102 | + (0.75,0.0, 0.0), |
| 103 | + (1.0, 0.0, 0.0)) |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | +blue_red1 = LinearSegmentedColormap('BlueRed1', cdict1) |
| 108 | +blue_red2 = LinearSegmentedColormap('BlueRed2', cdict2) |
| 109 | +blue_red3 = LinearSegmentedColormap('BlueRed3', cdict3) |
| 110 | + |
| 111 | +x = np.arange(0, np.pi, 0.1) |
| 112 | +y = np.arange(0, 2*np.pi, 0.1) |
| 113 | +X, Y = np.meshgrid(x,y) |
| 114 | +Z = np.cos(X) * np.sin(Y) |
| 115 | + |
| 116 | +plt.figure(figsize=(10,4)) |
| 117 | +plt.subplots_adjust(wspace=0.3) |
| 118 | + |
| 119 | +plt.subplot(1,3,1) |
| 120 | +plt.imshow(Z, interpolation='nearest', cmap=blue_red1) |
| 121 | +plt.colorbar() |
| 122 | + |
| 123 | +plt.subplot(1,3,2) |
| 124 | +plt.imshow(Z, interpolation='nearest', cmap=blue_red2) |
| 125 | +plt.colorbar() |
| 126 | + |
| 127 | +plt.subplot(1,3,3) |
| 128 | +plt.imshow(Z, interpolation='nearest', cmap=blue_red3) |
| 129 | +plt.colorbar() |
| 130 | + |
| 131 | +plt.suptitle('Custom Blue-Red colormaps') |
| 132 | + |
| 133 | +plt.show() |
| 134 | + |
0 commit comments