|
5 | 5 | from matplotlib.colors import LinearSegmentedColormap
|
6 | 6 |
|
7 | 7 | """
|
| 8 | +Creating a colormap from a list of colors |
| 9 | +----------------------------------------- |
| 10 | +Creating a colormap from a list of colors can be done with the `from_list` |
| 11 | +method of `LinearSegmentedColormap`. You must pass a list of RGB tuples that |
| 12 | +define the mixture of colors from 0 to 1. |
| 13 | +
|
| 14 | +
|
| 15 | +Creating custom colormaps |
| 16 | +------------------------- |
| 17 | +It is also possible to create a custom mapping for a colormap. This is |
| 18 | +accomplished by creating dictionary that specifies how the RGB channels |
| 19 | +change from one end of the cmap to the other. |
8 | 20 |
|
9 | 21 | Example: suppose you want red to increase from 0 to 1 over the bottom
|
10 | 22 | half, green to do the same over the middle half, and blue over the top
|
|
57 | 69 | never used.
|
58 | 70 |
|
59 | 71 | """
|
| 72 | +# Make some illustrative fake data: |
| 73 | + |
| 74 | +x = np.arange(0, np.pi, 0.1) |
| 75 | +y = np.arange(0, 2*np.pi, 0.1) |
| 76 | +X, Y = np.meshgrid(x, y) |
| 77 | +Z = np.cos(X) * np.sin(Y) * 10 |
| 78 | + |
| 79 | + |
| 80 | +# --- Colormaps from a list --- |
60 | 81 |
|
| 82 | +colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)] # R -> G -> B |
| 83 | +n_bins = [3, 6, 10, 100] # Discretizes the interpolation into bins |
| 84 | +cmap_name = 'my_list' |
| 85 | +fig, axs = plt.subplots(2, 2, figsize=(6, 9)) |
| 86 | +fig.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05) |
| 87 | +for n_bin, ax in zip(n_bins, axs.ravel()): |
| 88 | + # Create the colormap |
| 89 | + cm = LinearSegmentedColormap.from_list( |
| 90 | + cmap_name, colors, N=n_bin) |
| 91 | + # Fewer bins will result in "coarser" colomap interpolation |
| 92 | + im = ax.imshow(Z, interpolation='nearest', origin='lower', cmap=cm) |
| 93 | + ax.set_title("N bins: %s" % n_bin) |
| 94 | + fig.colorbar(im, ax=ax) |
| 95 | + |
| 96 | + |
| 97 | +# --- Custom colormaps --- |
61 | 98 |
|
62 | 99 | cdict1 = {'red': ((0.0, 0.0, 0.0),
|
63 | 100 | (0.5, 0.0, 0.1),
|
|
132 | 169 | plt.register_cmap(name='BlueRed3', data=cdict3) # optional lut kwarg
|
133 | 170 | plt.register_cmap(name='BlueRedAlpha', data=cdict4)
|
134 | 171 |
|
135 |
| -# Make some illustrative fake data: |
136 |
| - |
137 |
| -x = np.arange(0, np.pi, 0.1) |
138 |
| -y = np.arange(0, 2*np.pi, 0.1) |
139 |
| -X, Y = np.meshgrid(x, y) |
140 |
| -Z = np.cos(X) * np.sin(Y) * 10 |
141 |
| - |
142 | 172 | # Make the figure:
|
143 | 173 |
|
144 |
| -plt.figure(figsize=(6, 9)) |
145 |
| -plt.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05) |
| 174 | +fig, axs = plt.subplots(2, 2, figsize=(6, 9)) |
| 175 | +fig.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05) |
146 | 176 |
|
147 | 177 | # Make 4 subplots:
|
148 | 178 |
|
149 |
| -plt.subplot(2, 2, 1) |
150 |
| -plt.imshow(Z, interpolation='nearest', cmap=blue_red1) |
151 |
| -plt.colorbar() |
| 179 | +im1 = axs[0, 0].imshow(Z, interpolation='nearest', cmap=blue_red1) |
| 180 | +fig.colorbar(im1, ax=axs[0, 0]) |
152 | 181 |
|
153 |
| -plt.subplot(2, 2, 2) |
154 | 182 | cmap = plt.get_cmap('BlueRed2')
|
155 |
| -plt.imshow(Z, interpolation='nearest', cmap=cmap) |
156 |
| -plt.colorbar() |
| 183 | +im2 = axs[1, 0].imshow(Z, interpolation='nearest', cmap=cmap) |
| 184 | +fig.colorbar(im2, ax=axs[1, 0]) |
157 | 185 |
|
158 | 186 | # Now we will set the third cmap as the default. One would
|
159 | 187 | # not normally do this in the middle of a script like this;
|
160 | 188 | # it is done here just to illustrate the method.
|
161 | 189 |
|
162 | 190 | plt.rcParams['image.cmap'] = 'BlueRed3'
|
163 | 191 |
|
164 |
| -plt.subplot(2, 2, 3) |
165 |
| -plt.imshow(Z, interpolation='nearest') |
166 |
| -plt.colorbar() |
167 |
| -plt.title("Alpha = 1") |
| 192 | +im3 = axs[0, 1].imshow(Z, interpolation='nearest') |
| 193 | +fig.colorbar(im3, ax=axs[0, 1]) |
| 194 | +axs[0, 1].set_title("Alpha = 1") |
168 | 195 |
|
169 | 196 | # Or as yet another variation, we can replace the rcParams
|
170 | 197 | # specification *before* the imshow with the following *after*
|
|
173 | 200 | # image-like item plotted via pyplot, if any.
|
174 | 201 | #
|
175 | 202 |
|
176 |
| -plt.subplot(2, 2, 4) |
177 | 203 | # Draw a line with low zorder so it will be behind the image.
|
178 |
| -plt.plot([0, 10*np.pi], [0, 20*np.pi], color='c', lw=20, zorder=-1) |
| 204 | +axs[1, 1].plot([0, 10*np.pi], [0, 20*np.pi], color='c', lw=20, zorder=-1) |
179 | 205 |
|
180 |
| -plt.imshow(Z, interpolation='nearest') |
181 |
| -plt.colorbar() |
| 206 | +im4 = axs[1, 1].imshow(Z, interpolation='nearest') |
| 207 | +fig.colorbar(im4, ax=axs[1, 1]) |
182 | 208 |
|
183 | 209 | # Here it is: changing the colormap for the current image and its
|
184 | 210 | # colorbar after they have been plotted.
|
185 |
| -plt.set_cmap('BlueRedAlpha') |
186 |
| -plt.title("Varying alpha") |
| 211 | +im4.set_cmap('BlueRedAlpha') |
| 212 | +axs[1, 1].set_title("Varying alpha") |
187 | 213 | #
|
188 | 214 |
|
189 |
| -plt.suptitle('Custom Blue-Red colormaps', fontsize=16) |
| 215 | +fig.suptitle('Custom Blue-Red colormaps', fontsize=16) |
190 | 216 |
|
191 | 217 | plt.show()
|
0 commit comments