|
103 | 103 | (1.0, 0.0, 0.0))
|
104 | 104 | }
|
105 | 105 |
|
| 106 | +# Make a modified version of cdict3 with some transparency |
| 107 | +# in the middle of the range. |
| 108 | +cdict4 = cdict3.copy() |
| 109 | +cdict4['alpha'] = ((0.0, 1.0, 1.0), |
| 110 | + # (0.25,1.0, 1.0), |
| 111 | + (0.5, 0.3, 0.3), |
| 112 | + # (0.75,1.0, 1.0), |
| 113 | + (1.0, 1.0, 1.0)) |
| 114 | + |
| 115 | + |
106 | 116 | # Now we will use this example to illustrate 3 ways of
|
107 | 117 | # handling custom colormaps.
|
108 | 118 | # First, the most direct and explicit:
|
|
121 | 131 | # leave everything to register_cmap:
|
122 | 132 |
|
123 | 133 | plt.register_cmap(name='BlueRed3', data=cdict3) # optional lut kwarg
|
| 134 | +plt.register_cmap(name='BlueRedAlpha', data=cdict4) |
| 135 | + |
| 136 | +# Make some illustrative fake data: |
124 | 137 |
|
125 | 138 | x = np.arange(0, np.pi, 0.1)
|
126 | 139 | y = np.arange(0, 2*np.pi, 0.1)
|
127 | 140 | X, Y = np.meshgrid(x,y)
|
128 |
| -Z = np.cos(X) * np.sin(Y) |
| 141 | +Z = np.cos(X) * np.sin(Y) * 10 |
| 142 | + |
| 143 | +# Make the figure: |
129 | 144 |
|
130 |
| -plt.figure(figsize=(10,4)) |
131 |
| -plt.subplots_adjust(wspace=0.3) |
| 145 | +plt.figure(figsize=(6,9)) |
| 146 | +plt.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05) |
132 | 147 |
|
133 |
| -plt.subplot(1,3,1) |
| 148 | +# Make 4 subplots: |
| 149 | + |
| 150 | +plt.subplot(2,2,1) |
134 | 151 | plt.imshow(Z, interpolation='nearest', cmap=blue_red1)
|
135 | 152 | plt.colorbar()
|
136 | 153 |
|
137 |
| -plt.subplot(1,3,2) |
| 154 | +plt.subplot(2,2,2) |
138 | 155 | cmap = plt.get_cmap('BlueRed2')
|
139 | 156 | plt.imshow(Z, interpolation='nearest', cmap=cmap)
|
140 | 157 | plt.colorbar()
|
|
145 | 162 |
|
146 | 163 | plt.rcParams['image.cmap'] = 'BlueRed3'
|
147 | 164 |
|
148 |
| -# Also see below for an alternative, particularly for |
149 |
| -# interactive use. |
150 |
| - |
151 |
| -plt.subplot(1,3,3) |
| 165 | +plt.subplot(2,2,3) |
152 | 166 | plt.imshow(Z, interpolation='nearest')
|
153 | 167 | plt.colorbar()
|
| 168 | +plt.title("Alpha = 1") |
154 | 169 |
|
155 |
| -# Or as yet another variation, we could replace the rcParams |
| 170 | +# Or as yet another variation, we can replace the rcParams |
156 | 171 | # specification *before* the imshow with the following *after*
|
157 |
| -# imshow: |
158 |
| -# |
159 |
| -# plt.set_cmap('BlueRed3') |
160 |
| -# |
| 172 | +# imshow. |
161 | 173 | # This sets the new default *and* sets the colormap of the last
|
162 | 174 | # image-like item plotted via pyplot, if any.
|
| 175 | +# |
163 | 176 |
|
| 177 | +plt.subplot(2,2,4) |
| 178 | +# Draw a line with low zorder so it will be behind the image. |
| 179 | +plt.plot([0, 10*np.pi], [0, 20*np.pi], color='c', lw=20, zorder=-1) |
| 180 | + |
| 181 | +plt.imshow(Z, interpolation='nearest') |
| 182 | +plt.colorbar() |
| 183 | + |
| 184 | +# Here it is: changing the colormap for the current image and its |
| 185 | +# colorbar after they have been plotted. |
| 186 | +plt.set_cmap('BlueRedAlpha') |
| 187 | +plt.title("Varying alpha") |
| 188 | +# |
164 | 189 |
|
165 |
| -plt.suptitle('Custom Blue-Red colormaps') |
| 190 | +plt.suptitle('Custom Blue-Red colormaps', fontsize=16) |
166 | 191 |
|
167 | 192 | plt.show()
|
168 | 193 |
|
0 commit comments