2 import matplotlib.pyplot
as plt
3 from matplotlib.colors
import LinearSegmentedColormap
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. 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. 19 Example: suppose you want red to increase from 0 to 1 over the bottom 20 half, green to do the same over the middle half, and blue over the top 21 half. Then you would use: 23 cdict = {'red': ((0.0, 0.0, 0.0), 27 'green': ((0.0, 0.0, 0.0), 32 'blue': ((0.0, 0.0, 0.0), 36 If, as in this example, there are no discontinuities in the r, g, and b 37 components, then it is quite simple: the second and third element of 38 each tuple, above, is the same--call it "y". The first element ("x") 39 defines interpolation intervals over the full range of 0 to 1, and it 40 must span that whole range. In other words, the values of x divide the 41 0-to-1 range into a set of segments, and y gives the end-point color 42 values for each segment. 44 Now consider the green. cdict['green'] is saying that for 45 0 <= x <= 0.25, y is zero; no green. 46 0.25 < x <= 0.75, y varies linearly from 0 to 1. 47 x > 0.75, y remains at 1, full green. 49 If there are discontinuities, then it is a little more complicated. 50 Label the 3 elements in each row in the cdict entry for a given color as 51 (x, y0, y1). Then for values of x between x[i] and x[i+1] the color 52 value is interpolated between y1[i] and y0[i+1]. 54 Going back to the cookbook example, look at cdict['red']; because y0 != 55 y1, it is saying that for x from 0 to 0.5, red increases from 0 to 1, 56 but then it jumps down, so that for x from 0.5 to 1, red increases from 57 0.7 to 1. Green ramps from 0 to 1 as x goes from 0 to 0.5, then jumps 58 back to 0, and ramps back to 1 as x goes from 0.5 to 1. 65 Above is an attempt to show that for x in the range x[i] to x[i+1], the 66 interpolation is between y1[i] and y0[i+1]. So, y0[0] and y1[-1] are 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
80 colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]
81 n_bins = [3, 6, 10, 100]
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()):
87 cm = LinearSegmentedColormap.from_list(
88 cmap_name, colors, N=n_bin)
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)
97 cdict1 = {
'red': ((0.0, 0.0, 0.0),
101 'green': ((0.0, 0.0, 0.0),
104 'blue': ((0.0, 0.0, 1.0),
109 cdict2 = {
'red': ((0.0, 0.0, 0.0),
113 'green': ((0.0, 0.0, 0.0),
116 'blue': ((0.0, 0.0, 0.1),
121 cdict3 = {
'red': ((0.0, 0.0, 0.0),
127 'green': ((0.0, 0.0, 0.0),
133 'blue': ((0.0, 0.0, 0.4),
142 cdict4 = cdict3.copy()
143 cdict4[
'alpha'] = ((0.0, 1.0, 1.0),
154 blue_red1 = LinearSegmentedColormap(
'BlueRed1', cdict1)
161 blue_red2 = LinearSegmentedColormap(
'BlueRed2', cdict2)
162 plt.register_cmap(cmap=blue_red2)
167 plt.register_cmap(name=
'BlueRed3', data=cdict3)
168 plt.register_cmap(name=
'BlueRedAlpha', data=cdict4)
auto zip(Iterables &&...iterables)
Range-for loop helper iterating across many collections at the same time.