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 b380fd7

Browse filesBrowse files
committed
adding from_list to custom cmap tutorial
1 parent a8ad349 commit b380fd7
Copy full SHA for b380fd7

File tree

Expand file treeCollapse file tree

1 file changed

+35
-7
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+35
-7
lines changed

‎examples/pylab_examples/custom_cmap.py

Copy file name to clipboardExpand all lines: examples/pylab_examples/custom_cmap.py
+35-7Lines changed: 35 additions & 7 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,30 @@
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 = [2, 5, 10, 100] # Discretizes the interpolation into bins
82+
cmap_name = 'my_list'
83+
f, axs = plt.subplots(1, 4, figsize=(10, 5))
84+
for n_bin, ax in zip(n_bins, axs):
85+
# Create the colormap
86+
cm = LinearSegmentedColormap.from_list(
87+
cmap_name, colors, N=n_bin)
88+
# Fewer bins will result in "coarser" colomap interpolation
89+
im = ax.imshow(Z, interpolation='nearest', cmap=cm)
90+
ax.set_title("N bins: %s" % n_bin)
91+
92+
93+
# --- Custom colormaps ---
5994

6095
cdict1 = {'red': ((0.0, 0.0, 0.0),
6196
(0.5, 0.0, 0.1),
@@ -130,13 +165,6 @@
130165
plt.register_cmap(name='BlueRed3', data=cdict3) # optional lut kwarg
131166
plt.register_cmap(name='BlueRedAlpha', data=cdict4)
132167

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-
140168
# Make the figure:
141169

142170
plt.figure(figsize=(6, 9))

0 commit comments

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