1
1
"""
2
- *************************************************
3
- Manipulating and Creating Colormaps in Matplotlib
4
- *************************************************
2
+ ********************************
3
+ Creating Colormaps in Matplotlib
4
+ ********************************
5
5
6
6
Matplotlib colormaps are implimented as a class, which makes them quite
7
- flexible, but ocasionally opaque to users as to how to create and/or
7
+ flexible, but opaque to users as to how to create and/or
8
8
manipulate them. This opacity is not helped in the library by the fact that
9
9
the named colormaps are accessed via `.matplotlib.cm.get_cmap` module, whereas
10
- the colormap class itself is defined in `.matplotlin.colors.Colormap`!
10
+ the colormap class itself is defined in `.matplotlib.colors.Colormap`!
11
+
12
+ Fortunately, the way to create colormaps is quite straight forward, by creating
13
+ an instance of class `.ListedColormap` using a Nx4 numpy array of values
14
+ between 0 and 1 to represent the RGBA values of the colormap.
11
15
12
16
Getting colormaps and accessing their values
13
17
============================================
16
20
:doc:`/tutorials/colors/colormaps` requires the use of
17
21
`.matplotlib.cm.get_cmap`, which returns a
18
22
:class:`.matplotlib.colors.ListedColormap` object. The second argument gives
19
- the size of the list of colors used to define the colormap.
23
+ the size of the list of colors used to define the colormap, and below we
24
+ use a modest value of 12 so there are not a lot of values to look at.
20
25
"""
21
26
22
27
import numpy as np
30
35
print (viridis )
31
36
32
37
##############################################################################
33
- # This list of colors can be directly accessed using the ``colors`` property,
34
- # or it can be indirectly acccessed by calling the object. Note that the list
35
- # is of the form of an RGBA Nx4 array, where N is the length of the colormap.
38
+ # The object ``viridis`` is a callable, that when passed a float between
39
+ # 0 and 1 returns an RGBA value from the colormap:
40
+
41
+ print (viridis (0.56 ))
42
+
43
+ ##############################################################################
44
+ # The list of colors that comprise the colormap can be directly accessed using
45
+ # the ``colors`` property,
46
+ # or it can be acccessed indirectly by calling ``viridis`` with an array
47
+ # of values matching the length of the colormap. Note that the returned list
48
+ # is in the form of an RGBA Nx4 array, where N is the length of the colormap.
36
49
37
50
print ('viridis.colors' , viridis .colors )
38
51
print ('viridis(range(12))' , viridis (range (12 )))
45
58
print ('viridis(np.linspace(0, 1, 15))' , viridis (np .linspace (0 , 1 , 15 )))
46
59
47
60
##############################################################################
48
- # Creating a new ListedColormap: Colormap carpentry
49
- # =================================================
61
+ # Creating a new ListedColormap
62
+ # =============================
50
63
#
51
64
# This is essential the inverse operation of the above where we supply a
52
65
# Nx4 numpy array with all values between 0 and 1,
64
77
newcmp = ListedColormap (newcolors )
65
78
66
79
def plot_examples (cms ):
80
+ """
81
+ helper function to plot two colormaps
82
+ """
67
83
np .random .seed (19680801 )
68
84
data = np .random .randn (30 ,30 )
69
85
@@ -84,7 +100,7 @@ def plot_examples(cms):
84
100
plot_examples ([viridis , newcmp ])
85
101
86
102
##############################################################################
87
- # and we can easily paste together two colormaps:
103
+ # and we can easily concatenate two colormaps:
88
104
89
105
top = cm .get_cmap ('Oranges_r' , 128 )
90
106
bottom = cm .get_cmap ('Blues' , 128 )
@@ -94,12 +110,25 @@ def plot_examples(cms):
94
110
newcmp = ListedColormap (newcolors , name = 'OrangeBlue' )
95
111
plot_examples ([viridis , newcmp ])
96
112
113
+ ##############################################################################
114
+ # Of course we need not start from a named colormap, we just need to create
115
+ # the Nx4 array to pass to `.ListedColormap`. Here we create a
116
+ # brown colormap that goes to white....
117
+
118
+ N = 256
119
+ vals = np .ones ((N , 4 ))
120
+ vals [:, 0 ] = np .linspace (90 / 256 , 1 , N )
121
+ vals [:, 1 ] = np .linspace (39 / 256 , 1 , N )
122
+ vals [:, 2 ] = np .linspace (41 / 256 , 1 , N )
123
+ newcmp = ListedColormap (vals )
124
+ plot_examples ([viridis , newcmp ])
125
+
97
126
##############################################################################
98
127
# LinearSegmented colormaps
99
128
# =========================
100
129
#
101
- # LinearSegmented colormaps are an alternate way to specify colormaps that
102
- # specify anchor points for linear ramps for each of RGB and optionally, alpha
130
+ # `.LinearSegmentedColormap` have an alternate way to specify colormaps that
131
+ # specify anchor points for linear ramps for each of RGB, and optionally, alpha
103
132
# (RGBA).
104
133
#
105
134
# The format to specify these colormaps is a bit complicated to allow
@@ -142,8 +171,8 @@ def plot_linearmap(cdict):
142
171
# element of the first anchor and the first element of the second anchor.
143
172
144
173
cdict ['red' ] = [[0.0 , 0.0 , 0.3 ],
145
- [0.5 , 1.0 , 0.9 ],
146
- [1.0 , 1.0 , 1.0 ]]
174
+ [0.5 , 1.0 , 0.9 ],
175
+ [1.0 , 1.0 , 1.0 ]]
147
176
plot_linearmap (cdict )
148
177
149
178
@@ -164,6 +193,4 @@ def plot_linearmap(cdict):
164
193
matplotlib .colors .LinearSegmentedColormap
165
194
matplotlib .colors .ListedColormap
166
195
matplotlib .cm
167
- matplotlib .cm .ScalarMappable .get_cmap
168
- matplotlib .pyplot .register_cmap
169
- matplotlib .cm .register_cmap
196
+ matplotlib .cm .get_cmap
0 commit comments