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 c936ef5

Browse filesBrowse files
authored
Merge pull request #23393 from QuLogic/custom-cmap
Clean up formatting of custom cmap example
2 parents 89331ee + bd001e4 commit c936ef5
Copy full SHA for c936ef5

File tree

Expand file treeCollapse file tree

1 file changed

+135
-91
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+135
-91
lines changed

‎examples/color/custom_cmap.py

Copy file name to clipboardExpand all lines: examples/color/custom_cmap.py
+135-91Lines changed: 135 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
1414
Creating custom colormaps
15-
-------------------------
15+
=========================
1616
It is also possible to create a custom mapping for a colormap. This is
1717
accomplished by creating dictionary that specifies how the RGB channels
1818
change from one end of the cmap to the other.
@@ -21,51 +21,78 @@
2121
half, green to do the same over the middle half, and blue over the top
2222
half. Then you would use::
2323
24-
cdict = {'red': ((0.0, 0.0, 0.0),
25-
(0.5, 1.0, 1.0),
26-
(1.0, 1.0, 1.0)),
27-
28-
'green': ((0.0, 0.0, 0.0),
29-
(0.25, 0.0, 0.0),
30-
(0.75, 1.0, 1.0),
31-
(1.0, 1.0, 1.0)),
32-
33-
'blue': ((0.0, 0.0, 0.0),
34-
(0.5, 0.0, 0.0),
35-
(1.0, 1.0, 1.0))}
24+
cdict = {
25+
'red': (
26+
(0.0, 0.0, 0.0),
27+
(0.5, 1.0, 1.0),
28+
(1.0, 1.0, 1.0),
29+
),
30+
'green': (
31+
(0.0, 0.0, 0.0),
32+
(0.25, 0.0, 0.0),
33+
(0.75, 1.0, 1.0),
34+
(1.0, 1.0, 1.0),
35+
),
36+
'blue': (
37+
(0.0, 0.0, 0.0),
38+
(0.5, 0.0, 0.0),
39+
(1.0, 1.0, 1.0),
40+
)
41+
}
3642
3743
If, as in this example, there are no discontinuities in the r, g, and b
3844
components, then it is quite simple: the second and third element of
39-
each tuple, above, is the same--call it "y". The first element ("x")
45+
each tuple, above, is the same--call it "``y``". The first element ("``x``")
4046
defines interpolation intervals over the full range of 0 to 1, and it
41-
must span that whole range. In other words, the values of x divide the
42-
0-to-1 range into a set of segments, and y gives the end-point color
47+
must span that whole range. In other words, the values of ``x`` divide the
48+
0-to-1 range into a set of segments, and ``y`` gives the end-point color
4349
values for each segment.
4450
45-
Now consider the green. cdict['green'] is saying that for
46-
0 <= x <= 0.25, y is zero; no green.
47-
0.25 < x <= 0.75, y varies linearly from 0 to 1.
48-
x > 0.75, y remains at 1, full green.
49-
50-
If there are discontinuities, then it is a little more complicated.
51-
Label the 3 elements in each row in the cdict entry for a given color as
52-
(x, y0, y1). Then for values of x between x[i] and x[i+1] the color
53-
value is interpolated between y1[i] and y0[i+1].
54-
55-
Going back to the cookbook example, look at cdict['red']; because y0 !=
56-
y1, it is saying that for x from 0 to 0.5, red increases from 0 to 1,
57-
but then it jumps down, so that for x from 0.5 to 1, red increases from
58-
0.7 to 1. Green ramps from 0 to 1 as x goes from 0 to 0.5, then jumps
59-
back to 0, and ramps back to 1 as x goes from 0.5 to 1.::
51+
Now consider the green, ``cdict['green']`` is saying that for:
52+
53+
- 0 <= ``x`` <= 0.25, ``y`` is zero; no green.
54+
- 0.25 < ``x`` <= 0.75, ``y`` varies linearly from 0 to 1.
55+
- 0.75 < ``x`` <= 1, ``y`` remains at 1, full green.
56+
57+
If there are discontinuities, then it is a little more complicated. Label the 3
58+
elements in each row in the ``cdict`` entry for a given color as ``(x, y0,
59+
y1)``. Then for values of ``x`` between ``x[i]`` and ``x[i+1]`` the color value
60+
is interpolated between ``y1[i]`` and ``y0[i+1]``.
61+
62+
Going back to a cookbook example::
63+
64+
cdict = {
65+
'red': (
66+
(0.0, 0.0, 0.0),
67+
(0.5, 1.0, 0.7),
68+
(1.0, 1.0, 1.0),
69+
),
70+
'green': (
71+
(0.0, 0.0, 0.0),
72+
(0.5, 1.0, 0.0),
73+
(1.0, 1.0, 1.0),
74+
),
75+
'blue': (
76+
(0.0, 0.0, 0.0),
77+
(0.5, 0.0, 0.0),
78+
(1.0, 1.0, 1.0),
79+
)
80+
}
81+
82+
and look at ``cdict['red'][1]``; because ``y0 != y1``, it is saying that for
83+
``x`` from 0 to 0.5, red increases from 0 to 1, but then it jumps down, so that
84+
for ``x`` from 0.5 to 1, red increases from 0.7 to 1. Green ramps from 0 to 1
85+
as ``x`` goes from 0 to 0.5, then jumps back to 0, and ramps back to 1 as ``x``
86+
goes from 0.5 to 1. ::
6087
6188
row i: x y0 y1
62-
/
6389
/
90+
/
6491
row i+1: x y0 y1
6592
66-
Above is an attempt to show that for x in the range x[i] to x[i+1], the
67-
interpolation is between y1[i] and y0[i+1]. So, y0[0] and y1[-1] are
68-
never used.
93+
Above is an attempt to show that for ``x`` in the range ``x[i]`` to ``x[i+1]``,
94+
the interpolation is between ``y1[i]`` and ``y0[i+1]``. So, ``y0[0]`` and
95+
``y1[-1]`` are never used.
6996
7097
"""
7198
import numpy as np
@@ -82,7 +109,8 @@
82109

83110

84111
###############################################################################
85-
# --- Colormaps from a list ---
112+
# Colormaps from a list
113+
# ---------------------
86114

87115
colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)] # R -> G -> B
88116
n_bins = [3, 6, 10, 100] # Discretizes the interpolation into bins
@@ -99,60 +127,79 @@
99127

100128

101129
###############################################################################
102-
# --- Custom colormaps ---
103-
104-
cdict1 = {'red': ((0.0, 0.0, 0.0),
105-
(0.5, 0.0, 0.1),
106-
(1.0, 1.0, 1.0)),
107-
108-
'green': ((0.0, 0.0, 0.0),
109-
(1.0, 0.0, 0.0)),
110-
111-
'blue': ((0.0, 0.0, 1.0),
112-
(0.5, 0.1, 0.0),
113-
(1.0, 0.0, 0.0))
114-
}
115-
116-
cdict2 = {'red': ((0.0, 0.0, 0.0),
117-
(0.5, 0.0, 1.0),
118-
(1.0, 0.1, 1.0)),
119-
120-
'green': ((0.0, 0.0, 0.0),
121-
(1.0, 0.0, 0.0)),
122-
123-
'blue': ((0.0, 0.0, 0.1),
124-
(0.5, 1.0, 0.0),
125-
(1.0, 0.0, 0.0))
126-
}
127-
128-
cdict3 = {'red': ((0.0, 0.0, 0.0),
129-
(0.25, 0.0, 0.0),
130-
(0.5, 0.8, 1.0),
131-
(0.75, 1.0, 1.0),
132-
(1.0, 0.4, 1.0)),
133-
134-
'green': ((0.0, 0.0, 0.0),
135-
(0.25, 0.0, 0.0),
136-
(0.5, 0.9, 0.9),
137-
(0.75, 0.0, 0.0),
138-
(1.0, 0.0, 0.0)),
139-
140-
'blue': ((0.0, 0.0, 0.4),
141-
(0.25, 1.0, 1.0),
142-
(0.5, 1.0, 0.8),
143-
(0.75, 0.0, 0.0),
144-
(1.0, 0.0, 0.0))
145-
}
130+
# Custom colormaps
131+
# ----------------
132+
133+
cdict1 = {
134+
'red': (
135+
(0.0, 0.0, 0.0),
136+
(0.5, 0.0, 0.1),
137+
(1.0, 1.0, 1.0),
138+
),
139+
'green': (
140+
(0.0, 0.0, 0.0),
141+
(1.0, 0.0, 0.0),
142+
),
143+
'blue': (
144+
(0.0, 0.0, 1.0),
145+
(0.5, 0.1, 0.0),
146+
(1.0, 0.0, 0.0),
147+
)
148+
}
149+
150+
cdict2 = {
151+
'red': (
152+
(0.0, 0.0, 0.0),
153+
(0.5, 0.0, 1.0),
154+
(1.0, 0.1, 1.0),
155+
),
156+
'green': (
157+
(0.0, 0.0, 0.0),
158+
(1.0, 0.0, 0.0),
159+
),
160+
'blue': (
161+
(0.0, 0.0, 0.1),
162+
(0.5, 1.0, 0.0),
163+
(1.0, 0.0, 0.0),
164+
)
165+
}
166+
167+
cdict3 = {
168+
'red': (
169+
(0.0, 0.0, 0.0),
170+
(0.25, 0.0, 0.0),
171+
(0.5, 0.8, 1.0),
172+
(0.75, 1.0, 1.0),
173+
(1.0, 0.4, 1.0),
174+
),
175+
'green': (
176+
(0.0, 0.0, 0.0),
177+
(0.25, 0.0, 0.0),
178+
(0.5, 0.9, 0.9),
179+
(0.75, 0.0, 0.0),
180+
(1.0, 0.0, 0.0),
181+
),
182+
'blue': (
183+
(0.0, 0.0, 0.4),
184+
(0.25, 1.0, 1.0),
185+
(0.5, 1.0, 0.8),
186+
(0.75, 0.0, 0.0),
187+
(1.0, 0.0, 0.0),
188+
)
189+
}
146190

147191
# Make a modified version of cdict3 with some transparency
148192
# in the middle of the range.
149-
cdict4 = {**cdict3,
150-
'alpha': ((0.0, 1.0, 1.0),
151-
# (0.25, 1.0, 1.0),
152-
(0.5, 0.3, 0.3),
153-
# (0.75, 1.0, 1.0),
154-
(1.0, 1.0, 1.0)),
155-
}
193+
cdict4 = {
194+
**cdict3,
195+
'alpha': (
196+
(0.0, 1.0, 1.0),
197+
# (0.25, 1.0, 1.0),
198+
(0.5, 0.3, 0.3),
199+
# (0.75, 1.0, 1.0),
200+
(1.0, 1.0, 1.0),
201+
),
202+
}
156203

157204

158205
###############################################################################
@@ -173,13 +220,11 @@
173220
mpl.colormaps.register(LinearSegmentedColormap('BlueRedAlpha', cdict4))
174221

175222
###############################################################################
176-
# Make the figure:
223+
# Make the figure, with 4 subplots:
177224

178225
fig, axs = plt.subplots(2, 2, figsize=(6, 9))
179226
fig.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05)
180227

181-
# Make 4 subplots:
182-
183228
im1 = axs[0, 0].imshow(Z, cmap=blue_red1)
184229
fig.colorbar(im1, ax=axs[0, 0])
185230

@@ -213,7 +258,6 @@
213258
# colorbar after they have been plotted.
214259
im4.set_cmap('BlueRedAlpha')
215260
axs[1, 1].set_title("Varying alpha")
216-
#
217261

218262
fig.suptitle('Custom Blue-Red colormaps', fontsize=16)
219263
fig.subplots_adjust(top=0.9)

0 commit comments

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