Skip to content

Commit 6298a74

Browse files
committed
Modify custom_cmap example to illustrate colormap with alpha
1 parent a44ed2f commit 6298a74

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

examples/pylab_examples/custom_cmap.py

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@
103103
(1.0, 0.0, 0.0))
104104
}
105105

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+
106116
# Now we will use this example to illustrate 3 ways of
107117
# handling custom colormaps.
108118
# First, the most direct and explicit:
@@ -121,20 +131,27 @@
121131
# leave everything to register_cmap:
122132

123133
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:
124137

125138
x = np.arange(0, np.pi, 0.1)
126139
y = np.arange(0, 2*np.pi, 0.1)
127140
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:
129144

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)
132147

133-
plt.subplot(1,3,1)
148+
# Make 4 subplots:
149+
150+
plt.subplot(2,2,1)
134151
plt.imshow(Z, interpolation='nearest', cmap=blue_red1)
135152
plt.colorbar()
136153

137-
plt.subplot(1,3,2)
154+
plt.subplot(2,2,2)
138155
cmap = plt.get_cmap('BlueRed2')
139156
plt.imshow(Z, interpolation='nearest', cmap=cmap)
140157
plt.colorbar()
@@ -145,24 +162,32 @@
145162

146163
plt.rcParams['image.cmap'] = 'BlueRed3'
147164

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)
152166
plt.imshow(Z, interpolation='nearest')
153167
plt.colorbar()
168+
plt.title("Alpha = 1")
154169

155-
# Or as yet another variation, we could replace the rcParams
170+
# Or as yet another variation, we can replace the rcParams
156171
# specification *before* the imshow with the following *after*
157-
# imshow:
158-
#
159-
# plt.set_cmap('BlueRed3')
160-
#
172+
# imshow.
161173
# This sets the new default *and* sets the colormap of the last
162174
# image-like item plotted via pyplot, if any.
175+
#
163176

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+
#
164189

165-
plt.suptitle('Custom Blue-Red colormaps')
190+
plt.suptitle('Custom Blue-Red colormaps', fontsize=16)
166191

167192
plt.show()
168193

0 commit comments

Comments
 (0)