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