Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9e5b18a

Browse files
committed
Merge pull request #6662 from choldgraf/tutorial_fromlist_cmap
DOC: adding from_list to custom cmap tutorial
1 parent 84f3a6f commit 9e5b18a

File tree

1 file changed

+52
-26
lines changed

1 file changed

+52
-26
lines changed

examples/pylab_examples/custom_cmap.py

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55
from matplotlib.colors import LinearSegmentedColormap
66

77
"""
8+
Creating a colormap from a list of colors
9+
-----------------------------------------
10+
Creating a colormap from a list of colors can be done with the `from_list`
11+
method of `LinearSegmentedColormap`. You must pass a list of RGB tuples that
12+
define the mixture of colors from 0 to 1.
13+
14+
15+
Creating custom colormaps
16+
-------------------------
17+
It is also possible to create a custom mapping for a colormap. This is
18+
accomplished by creating dictionary that specifies how the RGB channels
19+
change from one end of the cmap to the other.
820
921
Example: suppose you want red to increase from 0 to 1 over the bottom
1022
half, green to do the same over the middle half, and blue over the top
@@ -57,7 +69,32 @@
5769
never used.
5870
5971
"""
72+
# Make some illustrative fake data:
73+
74+
x = np.arange(0, np.pi, 0.1)
75+
y = np.arange(0, 2*np.pi, 0.1)
76+
X, Y = np.meshgrid(x, y)
77+
Z = np.cos(X) * np.sin(Y) * 10
78+
79+
80+
# --- Colormaps from a list ---
6081

82+
colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)] # R -> G -> B
83+
n_bins = [3, 6, 10, 100] # Discretizes the interpolation into bins
84+
cmap_name = 'my_list'
85+
fig, axs = plt.subplots(2, 2, figsize=(6, 9))
86+
fig.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05)
87+
for n_bin, ax in zip(n_bins, axs.ravel()):
88+
# Create the colormap
89+
cm = LinearSegmentedColormap.from_list(
90+
cmap_name, colors, N=n_bin)
91+
# Fewer bins will result in "coarser" colomap interpolation
92+
im = ax.imshow(Z, interpolation='nearest', origin='lower', cmap=cm)
93+
ax.set_title("N bins: %s" % n_bin)
94+
fig.colorbar(im, ax=ax)
95+
96+
97+
# --- Custom colormaps ---
6198

6299
cdict1 = {'red': ((0.0, 0.0, 0.0),
63100
(0.5, 0.0, 0.1),
@@ -132,39 +169,29 @@
132169
plt.register_cmap(name='BlueRed3', data=cdict3) # optional lut kwarg
133170
plt.register_cmap(name='BlueRedAlpha', data=cdict4)
134171

135-
# Make some illustrative fake data:
136-
137-
x = np.arange(0, np.pi, 0.1)
138-
y = np.arange(0, 2*np.pi, 0.1)
139-
X, Y = np.meshgrid(x, y)
140-
Z = np.cos(X) * np.sin(Y) * 10
141-
142172
# Make the figure:
143173

144-
plt.figure(figsize=(6, 9))
145-
plt.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05)
174+
fig, axs = plt.subplots(2, 2, figsize=(6, 9))
175+
fig.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05)
146176

147177
# Make 4 subplots:
148178

149-
plt.subplot(2, 2, 1)
150-
plt.imshow(Z, interpolation='nearest', cmap=blue_red1)
151-
plt.colorbar()
179+
im1 = axs[0, 0].imshow(Z, interpolation='nearest', cmap=blue_red1)
180+
fig.colorbar(im1, ax=axs[0, 0])
152181

153-
plt.subplot(2, 2, 2)
154182
cmap = plt.get_cmap('BlueRed2')
155-
plt.imshow(Z, interpolation='nearest', cmap=cmap)
156-
plt.colorbar()
183+
im2 = axs[1, 0].imshow(Z, interpolation='nearest', cmap=cmap)
184+
fig.colorbar(im2, ax=axs[1, 0])
157185

158186
# Now we will set the third cmap as the default. One would
159187
# not normally do this in the middle of a script like this;
160188
# it is done here just to illustrate the method.
161189

162190
plt.rcParams['image.cmap'] = 'BlueRed3'
163191

164-
plt.subplot(2, 2, 3)
165-
plt.imshow(Z, interpolation='nearest')
166-
plt.colorbar()
167-
plt.title("Alpha = 1")
192+
im3 = axs[0, 1].imshow(Z, interpolation='nearest')
193+
fig.colorbar(im3, ax=axs[0, 1])
194+
axs[0, 1].set_title("Alpha = 1")
168195

169196
# Or as yet another variation, we can replace the rcParams
170197
# specification *before* the imshow with the following *after*
@@ -173,19 +200,18 @@
173200
# image-like item plotted via pyplot, if any.
174201
#
175202

176-
plt.subplot(2, 2, 4)
177203
# Draw a line with low zorder so it will be behind the image.
178-
plt.plot([0, 10*np.pi], [0, 20*np.pi], color='c', lw=20, zorder=-1)
204+
axs[1, 1].plot([0, 10*np.pi], [0, 20*np.pi], color='c', lw=20, zorder=-1)
179205

180-
plt.imshow(Z, interpolation='nearest')
181-
plt.colorbar()
206+
im4 = axs[1, 1].imshow(Z, interpolation='nearest')
207+
fig.colorbar(im4, ax=axs[1, 1])
182208

183209
# Here it is: changing the colormap for the current image and its
184210
# colorbar after they have been plotted.
185-
plt.set_cmap('BlueRedAlpha')
186-
plt.title("Varying alpha")
211+
im4.set_cmap('BlueRedAlpha')
212+
axs[1, 1].set_title("Varying alpha")
187213
#
188214

189-
plt.suptitle('Custom Blue-Red colormaps', fontsize=16)
215+
fig.suptitle('Custom Blue-Red colormaps', fontsize=16)
190216

191217
plt.show()

0 commit comments

Comments
 (0)