cmaps.py
Go to the documentation of this file.
1 import numpy as np
2 import matplotlib.pyplot as plt
3 from matplotlib.colors import LinearSegmentedColormap
4 
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.
18 
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:
22 
23 cdict = {'red': ((0.0, 0.0, 0.0),
24  (0.5, 1.0, 1.0),
25  (1.0, 1.0, 1.0)),
26 
27  'green': ((0.0, 0.0, 0.0),
28  (0.25, 0.0, 0.0),
29  (0.75, 1.0, 1.0),
30  (1.0, 1.0, 1.0)),
31 
32  'blue': ((0.0, 0.0, 0.0),
33  (0.5, 0.0, 0.0),
34  (1.0, 1.0, 1.0))}
35 
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.
43 
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.
48 
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].
53 
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.
59 
60 row i: x y0 y1
61  /
62  /
63 row i+1: x y0 y1
64 
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
67 never used.
68 
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 ---
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 ---
96 
97 cdict1 = {'red': ((0.0, 0.0, 0.0),
98  (0.5, 0.0, 0.1),
99  (1.0, 1.0, 1.0)),
100 
101  'green': ((0.0, 0.0, 0.0),
102  (1.0, 0.0, 0.0)),
103 
104  'blue': ((0.0, 0.0, 1.0),
105  (0.5, 0.1, 0.0),
106  (1.0, 0.0, 0.0))
107  }
108 
109 cdict2 = {'red': ((0.0, 0.0, 0.0),
110  (0.5, 0.0, 1.0),
111  (1.0, 0.1, 1.0)),
112 
113  'green': ((0.0, 0.0, 0.0),
114  (1.0, 0.0, 0.0)),
115 
116  'blue': ((0.0, 0.0, 0.1),
117  (0.5, 1.0, 0.0),
118  (1.0, 0.0, 0.0))
119  }
120 
121 cdict3 = {'red': ((0.0, 0.0, 0.0),
122  (0.25, 0.0, 0.0),
123  (0.5, 0.8, 1.0),
124  (0.75, 1.0, 1.0),
125  (1.0, 0.4, 1.0)),
126 
127  'green': ((0.0, 0.0, 0.0),
128  (0.25, 0.0, 0.0),
129  (0.5, 0.9, 0.9),
130  (0.75, 0.0, 0.0),
131  (1.0, 0.0, 0.0)),
132 
133  'blue': ((0.0, 0.0, 0.4),
134  (0.25, 1.0, 1.0),
135  (0.5, 1.0, 0.8),
136  (0.75, 0.0, 0.0),
137  (1.0, 0.0, 0.0))
138  }
139 
140 # Make a modified version of cdict3 with some transparency
141 # in the middle of the range.
142 cdict4 = cdict3.copy()
143 cdict4['alpha'] = ((0.0, 1.0, 1.0),
144  # (0.25,1.0, 1.0),
145  (0.5, 0.3, 0.3),
146  # (0.75,1.0, 1.0),
147  (1.0, 1.0, 1.0))
148 
149 
150 # Now we will use this example to illustrate 3 ways of
151 # handling custom colormaps.
152 # First, the most direct and explicit:
153 
154 blue_red1 = LinearSegmentedColormap('BlueRed1', cdict1)
155 
156 # Second, create the map explicitly and register it.
157 # Like the first method, this method works with any kind
158 # of Colormap, not just
159 # a LinearSegmentedColormap:
160 
161 blue_red2 = LinearSegmentedColormap('BlueRed2', cdict2)
162 plt.register_cmap(cmap=blue_red2)
163 
164 # Third, for LinearSegmentedColormap only,
165 # leave everything to register_cmap:
166 
167 plt.register_cmap(name='BlueRed3', data=cdict3) # optional lut kwarg
168 plt.register_cmap(name='BlueRedAlpha', data=cdict4)
169 
170 
auto zip(Iterables &&...iterables)
Range-for loop helper iterating across many collections at the same time.
Definition: zip.h:295