|
12 | 12 |
|
13 | 13 |
|
14 | 14 | Creating custom colormaps |
15 | | -------------------------- |
| 15 | +========================= |
16 | 16 | It is also possible to create a custom mapping for a colormap. This is |
17 | 17 | accomplished by creating dictionary that specifies how the RGB channels |
18 | 18 | change from one end of the cmap to the other. |
|
21 | 21 | half, green to do the same over the middle half, and blue over the top |
22 | 22 | half. Then you would use:: |
23 | 23 |
|
24 | | - cdict = {'red': ((0.0, 0.0, 0.0), |
25 | | - (0.5, 1.0, 1.0), |
26 | | - (1.0, 1.0, 1.0)), |
27 | | -
|
28 | | - 'green': ((0.0, 0.0, 0.0), |
29 | | - (0.25, 0.0, 0.0), |
30 | | - (0.75, 1.0, 1.0), |
31 | | - (1.0, 1.0, 1.0)), |
32 | | -
|
33 | | - 'blue': ((0.0, 0.0, 0.0), |
34 | | - (0.5, 0.0, 0.0), |
35 | | - (1.0, 1.0, 1.0))} |
| 24 | + cdict = { |
| 25 | + 'red': ( |
| 26 | + (0.0, 0.0, 0.0), |
| 27 | + (0.5, 1.0, 1.0), |
| 28 | + (1.0, 1.0, 1.0), |
| 29 | + ), |
| 30 | + 'green': ( |
| 31 | + (0.0, 0.0, 0.0), |
| 32 | + (0.25, 0.0, 0.0), |
| 33 | + (0.75, 1.0, 1.0), |
| 34 | + (1.0, 1.0, 1.0), |
| 35 | + ), |
| 36 | + 'blue': ( |
| 37 | + (0.0, 0.0, 0.0), |
| 38 | + (0.5, 0.0, 0.0), |
| 39 | + (1.0, 1.0, 1.0), |
| 40 | + ) |
| 41 | + } |
36 | 42 |
|
37 | 43 | If, as in this example, there are no discontinuities in the r, g, and b |
38 | 44 | components, then it is quite simple: the second and third element of |
39 | | -each tuple, above, is the same--call it "y". The first element ("x") |
| 45 | +each tuple, above, is the same--call it "``y``". The first element ("``x``") |
40 | 46 | defines interpolation intervals over the full range of 0 to 1, and it |
41 | | -must span that whole range. In other words, the values of x divide the |
42 | | -0-to-1 range into a set of segments, and y gives the end-point color |
| 47 | +must span that whole range. In other words, the values of ``x`` divide the |
| 48 | +0-to-1 range into a set of segments, and ``y`` gives the end-point color |
43 | 49 | values for each segment. |
44 | 50 |
|
45 | | -Now consider the green. cdict['green'] is saying that for |
46 | | -0 <= x <= 0.25, y is zero; no green. |
47 | | -0.25 < x <= 0.75, y varies linearly from 0 to 1. |
48 | | -x > 0.75, y remains at 1, full green. |
49 | | -
|
50 | | -If there are discontinuities, then it is a little more complicated. |
51 | | -Label the 3 elements in each row in the cdict entry for a given color as |
52 | | -(x, y0, y1). Then for values of x between x[i] and x[i+1] the color |
53 | | -value is interpolated between y1[i] and y0[i+1]. |
54 | | -
|
55 | | -Going back to the cookbook example, look at cdict['red']; because y0 != |
56 | | -y1, it is saying that for x from 0 to 0.5, red increases from 0 to 1, |
57 | | -but then it jumps down, so that for x from 0.5 to 1, red increases from |
58 | | -0.7 to 1. Green ramps from 0 to 1 as x goes from 0 to 0.5, then jumps |
59 | | -back to 0, and ramps back to 1 as x goes from 0.5 to 1.:: |
| 51 | +Now consider the green, ``cdict['green']`` is saying that for: |
| 52 | +
|
| 53 | +- 0 <= ``x`` <= 0.25, ``y`` is zero; no green. |
| 54 | +- 0.25 < ``x`` <= 0.75, ``y`` varies linearly from 0 to 1. |
| 55 | +- 0.75 < ``x`` <= 1, ``y`` remains at 1, full green. |
| 56 | +
|
| 57 | +If there are discontinuities, then it is a little more complicated. Label the 3 |
| 58 | +elements in each row in the ``cdict`` entry for a given color as ``(x, y0, |
| 59 | +y1)``. Then for values of ``x`` between ``x[i]`` and ``x[i+1]`` the color value |
| 60 | +is interpolated between ``y1[i]`` and ``y0[i+1]``. |
| 61 | +
|
| 62 | +Going back to a cookbook example:: |
| 63 | +
|
| 64 | + cdict = { |
| 65 | + 'red': ( |
| 66 | + (0.0, 0.0, 0.0), |
| 67 | + (0.5, 1.0, 0.7), |
| 68 | + (1.0, 1.0, 1.0), |
| 69 | + ), |
| 70 | + 'green': ( |
| 71 | + (0.0, 0.0, 0.0), |
| 72 | + (0.5, 1.0, 0.0), |
| 73 | + (1.0, 1.0, 1.0), |
| 74 | + ), |
| 75 | + 'blue': ( |
| 76 | + (0.0, 0.0, 0.0), |
| 77 | + (0.5, 0.0, 0.0), |
| 78 | + (1.0, 1.0, 1.0), |
| 79 | + ) |
| 80 | + } |
| 81 | +
|
| 82 | +and look at ``cdict['red'][1]``; because ``y0 != y1``, it is saying that for |
| 83 | +``x`` from 0 to 0.5, red increases from 0 to 1, but then it jumps down, so that |
| 84 | +for ``x`` from 0.5 to 1, red increases from 0.7 to 1. Green ramps from 0 to 1 |
| 85 | +as ``x`` goes from 0 to 0.5, then jumps back to 0, and ramps back to 1 as ``x`` |
| 86 | +goes from 0.5 to 1. :: |
60 | 87 |
|
61 | 88 | row i: x y0 y1 |
62 | | - / |
63 | 89 | / |
| 90 | + / |
64 | 91 | row i+1: x y0 y1 |
65 | 92 |
|
66 | | -Above is an attempt to show that for x in the range x[i] to x[i+1], the |
67 | | -interpolation is between y1[i] and y0[i+1]. So, y0[0] and y1[-1] are |
68 | | -never used. |
| 93 | +Above is an attempt to show that for ``x`` in the range ``x[i]`` to ``x[i+1]``, |
| 94 | +the interpolation is between ``y1[i]`` and ``y0[i+1]``. So, ``y0[0]`` and |
| 95 | +``y1[-1]`` are never used. |
69 | 96 |
|
70 | 97 | """ |
71 | 98 | import numpy as np |
|
82 | 109 |
|
83 | 110 |
|
84 | 111 | ############################################################################### |
85 | | -# --- Colormaps from a list --- |
| 112 | +# Colormaps from a list |
| 113 | +# --------------------- |
86 | 114 |
|
87 | 115 | colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)] # R -> G -> B |
88 | 116 | n_bins = [3, 6, 10, 100] # Discretizes the interpolation into bins |
|
99 | 127 |
|
100 | 128 |
|
101 | 129 | ############################################################################### |
102 | | -# --- Custom colormaps --- |
103 | | - |
104 | | -cdict1 = {'red': ((0.0, 0.0, 0.0), |
105 | | - (0.5, 0.0, 0.1), |
106 | | - (1.0, 1.0, 1.0)), |
107 | | - |
108 | | - 'green': ((0.0, 0.0, 0.0), |
109 | | - (1.0, 0.0, 0.0)), |
110 | | - |
111 | | - 'blue': ((0.0, 0.0, 1.0), |
112 | | - (0.5, 0.1, 0.0), |
113 | | - (1.0, 0.0, 0.0)) |
114 | | - } |
115 | | - |
116 | | -cdict2 = {'red': ((0.0, 0.0, 0.0), |
117 | | - (0.5, 0.0, 1.0), |
118 | | - (1.0, 0.1, 1.0)), |
119 | | - |
120 | | - 'green': ((0.0, 0.0, 0.0), |
121 | | - (1.0, 0.0, 0.0)), |
122 | | - |
123 | | - 'blue': ((0.0, 0.0, 0.1), |
124 | | - (0.5, 1.0, 0.0), |
125 | | - (1.0, 0.0, 0.0)) |
126 | | - } |
127 | | - |
128 | | -cdict3 = {'red': ((0.0, 0.0, 0.0), |
129 | | - (0.25, 0.0, 0.0), |
130 | | - (0.5, 0.8, 1.0), |
131 | | - (0.75, 1.0, 1.0), |
132 | | - (1.0, 0.4, 1.0)), |
133 | | - |
134 | | - 'green': ((0.0, 0.0, 0.0), |
135 | | - (0.25, 0.0, 0.0), |
136 | | - (0.5, 0.9, 0.9), |
137 | | - (0.75, 0.0, 0.0), |
138 | | - (1.0, 0.0, 0.0)), |
139 | | - |
140 | | - 'blue': ((0.0, 0.0, 0.4), |
141 | | - (0.25, 1.0, 1.0), |
142 | | - (0.5, 1.0, 0.8), |
143 | | - (0.75, 0.0, 0.0), |
144 | | - (1.0, 0.0, 0.0)) |
145 | | - } |
| 130 | +# Custom colormaps |
| 131 | +# ---------------- |
| 132 | + |
| 133 | +cdict1 = { |
| 134 | + 'red': ( |
| 135 | + (0.0, 0.0, 0.0), |
| 136 | + (0.5, 0.0, 0.1), |
| 137 | + (1.0, 1.0, 1.0), |
| 138 | + ), |
| 139 | + 'green': ( |
| 140 | + (0.0, 0.0, 0.0), |
| 141 | + (1.0, 0.0, 0.0), |
| 142 | + ), |
| 143 | + 'blue': ( |
| 144 | + (0.0, 0.0, 1.0), |
| 145 | + (0.5, 0.1, 0.0), |
| 146 | + (1.0, 0.0, 0.0), |
| 147 | + ) |
| 148 | +} |
| 149 | + |
| 150 | +cdict2 = { |
| 151 | + 'red': ( |
| 152 | + (0.0, 0.0, 0.0), |
| 153 | + (0.5, 0.0, 1.0), |
| 154 | + (1.0, 0.1, 1.0), |
| 155 | + ), |
| 156 | + 'green': ( |
| 157 | + (0.0, 0.0, 0.0), |
| 158 | + (1.0, 0.0, 0.0), |
| 159 | + ), |
| 160 | + 'blue': ( |
| 161 | + (0.0, 0.0, 0.1), |
| 162 | + (0.5, 1.0, 0.0), |
| 163 | + (1.0, 0.0, 0.0), |
| 164 | + ) |
| 165 | +} |
| 166 | + |
| 167 | +cdict3 = { |
| 168 | + 'red': ( |
| 169 | + (0.0, 0.0, 0.0), |
| 170 | + (0.25, 0.0, 0.0), |
| 171 | + (0.5, 0.8, 1.0), |
| 172 | + (0.75, 1.0, 1.0), |
| 173 | + (1.0, 0.4, 1.0), |
| 174 | + ), |
| 175 | + 'green': ( |
| 176 | + (0.0, 0.0, 0.0), |
| 177 | + (0.25, 0.0, 0.0), |
| 178 | + (0.5, 0.9, 0.9), |
| 179 | + (0.75, 0.0, 0.0), |
| 180 | + (1.0, 0.0, 0.0), |
| 181 | + ), |
| 182 | + 'blue': ( |
| 183 | + (0.0, 0.0, 0.4), |
| 184 | + (0.25, 1.0, 1.0), |
| 185 | + (0.5, 1.0, 0.8), |
| 186 | + (0.75, 0.0, 0.0), |
| 187 | + (1.0, 0.0, 0.0), |
| 188 | + ) |
| 189 | +} |
146 | 190 |
|
147 | 191 | # Make a modified version of cdict3 with some transparency |
148 | 192 | # in the middle of the range. |
149 | | -cdict4 = {**cdict3, |
150 | | - 'alpha': ((0.0, 1.0, 1.0), |
151 | | - # (0.25, 1.0, 1.0), |
152 | | - (0.5, 0.3, 0.3), |
153 | | - # (0.75, 1.0, 1.0), |
154 | | - (1.0, 1.0, 1.0)), |
155 | | - } |
| 193 | +cdict4 = { |
| 194 | + **cdict3, |
| 195 | + 'alpha': ( |
| 196 | + (0.0, 1.0, 1.0), |
| 197 | + # (0.25, 1.0, 1.0), |
| 198 | + (0.5, 0.3, 0.3), |
| 199 | + # (0.75, 1.0, 1.0), |
| 200 | + (1.0, 1.0, 1.0), |
| 201 | + ), |
| 202 | +} |
156 | 203 |
|
157 | 204 |
|
158 | 205 | ############################################################################### |
|
173 | 220 | mpl.colormaps.register(LinearSegmentedColormap('BlueRedAlpha', cdict4)) |
174 | 221 |
|
175 | 222 | ############################################################################### |
176 | | -# Make the figure: |
| 223 | +# Make the figure, with 4 subplots: |
177 | 224 |
|
178 | 225 | fig, axs = plt.subplots(2, 2, figsize=(6, 9)) |
179 | 226 | fig.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05) |
180 | 227 |
|
181 | | -# Make 4 subplots: |
182 | | - |
183 | 228 | im1 = axs[0, 0].imshow(Z, cmap=blue_red1) |
184 | 229 | fig.colorbar(im1, ax=axs[0, 0]) |
185 | 230 |
|
|
213 | 258 | # colorbar after they have been plotted. |
214 | 259 | im4.set_cmap('BlueRedAlpha') |
215 | 260 | axs[1, 1].set_title("Varying alpha") |
216 | | -# |
217 | 261 |
|
218 | 262 | fig.suptitle('Custom Blue-Red colormaps', fontsize=16) |
219 | 263 | fig.subplots_adjust(top=0.9) |
|
0 commit comments