Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5151cf5

Browse filesBrowse files
authored
Merge pull request #6662 from choldgraf/tutorial_fromlist_cmap
DOC: adding from_list to custom cmap tutorial
2 parents fbafa13 + 963ded8 commit 5151cf5
Copy full SHA for 5151cf5

File tree

Expand file treeCollapse file tree

1 file changed

+52
-26
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+52
-26
lines changed

‎examples/pylab_examples/custom_cmap.py

Copy file name to clipboardExpand all lines: examples/pylab_examples/custom_cmap.py
+52-26Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
from matplotlib.colors import LinearSegmentedColormap
44

55
"""
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.
618
719
Example: suppose you want red to increase from 0 to 1 over the bottom
820
half, green to do the same over the middle half, and blue over the top
@@ -55,7 +67,32 @@
5567
never used.
5668
5769
"""
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 ---
5879

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 ---
5996

6097
cdict1 = {'red': ((0.0, 0.0, 0.0),
6198
(0.5, 0.0, 0.1),
@@ -130,39 +167,29 @@
130167
plt.register_cmap(name='BlueRed3', data=cdict3) # optional lut kwarg
131168
plt.register_cmap(name='BlueRedAlpha', data=cdict4)
132169

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

142-
plt.figure(figsize=(6, 9))
143-
plt.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05)
172+
fig, axs = plt.subplots(2, 2, figsize=(6, 9))
173+
fig.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05)
144174

145175
# Make 4 subplots:
146176

147-
plt.subplot(2, 2, 1)
148-
plt.imshow(Z, interpolation='nearest', cmap=blue_red1)
149-
plt.colorbar()
177+
im1 = axs[0, 0].imshow(Z, interpolation='nearest', cmap=blue_red1)
178+
fig.colorbar(im1, ax=axs[0, 0])
150179

151-
plt.subplot(2, 2, 2)
152180
cmap = plt.get_cmap('BlueRed2')
153-
plt.imshow(Z, interpolation='nearest', cmap=cmap)
154-
plt.colorbar()
181+
im2 = axs[1, 0].imshow(Z, interpolation='nearest', cmap=cmap)
182+
fig.colorbar(im2, ax=axs[1, 0])
155183

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

160188
plt.rcParams['image.cmap'] = 'BlueRed3'
161189

162-
plt.subplot(2, 2, 3)
163-
plt.imshow(Z, interpolation='nearest')
164-
plt.colorbar()
165-
plt.title("Alpha = 1")
190+
im3 = axs[0, 1].imshow(Z, interpolation='nearest')
191+
fig.colorbar(im3, ax=axs[0, 1])
192+
axs[0, 1].set_title("Alpha = 1")
166193

167194
# Or as yet another variation, we can replace the rcParams
168195
# specification *before* the imshow with the following *after*
@@ -171,19 +198,18 @@
171198
# image-like item plotted via pyplot, if any.
172199
#
173200

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

178-
plt.imshow(Z, interpolation='nearest')
179-
plt.colorbar()
204+
im4 = axs[1, 1].imshow(Z, interpolation='nearest')
205+
fig.colorbar(im4, ax=axs[1, 1])
180206

181207
# Here it is: changing the colormap for the current image and its
182208
# colorbar after they have been plotted.
183-
plt.set_cmap('BlueRedAlpha')
184-
plt.title("Varying alpha")
209+
im4.set_cmap('BlueRedAlpha')
210+
axs[1, 1].set_title("Varying alpha")
185211
#
186212

187-
plt.suptitle('Custom Blue-Red colormaps', fontsize=16)
213+
fig.suptitle('Custom Blue-Red colormaps', fontsize=16)
188214

189215
plt.show()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.