11"""
2- *************************************************
3- Manipulating and Creating Colormaps in Matplotlib
4- *************************************************
2+ ********************************
3+ Creating Colormaps in Matplotlib
4+ ********************************
55
66Matplotlib 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
88manipulate them. This opacity is not helped in the library by the fact that
99the 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.
1115
1216Getting colormaps and accessing their values
1317============================================
1620:doc:`/tutorials/colors/colormaps` requires the use of
1721`.matplotlib.cm.get_cmap`, which returns a
1822: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.
2025"""
2126
2227import numpy as np
3035print (viridis )
3136
3237##############################################################################
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.
3649
3750print ('viridis.colors' , viridis .colors )
3851print ('viridis(range(12))' , viridis (range (12 )))
4558print ('viridis(np.linspace(0, 1, 15))' , viridis (np .linspace (0 , 1 , 15 )))
4659
4760##############################################################################
48- # Creating a new ListedColormap: Colormap carpentry
49- # =================================================
61+ # Creating a new ListedColormap
62+ # =============================
5063#
5164# This is essential the inverse operation of the above where we supply a
5265# Nx4 numpy array with all values between 0 and 1,
6477newcmp = ListedColormap (newcolors )
6578
6679def plot_examples (cms ):
80+ """
81+ helper function to plot two colormaps
82+ """
6783 np .random .seed (19680801 )
6884 data = np .random .randn (30 ,30 )
6985
@@ -84,7 +100,7 @@ def plot_examples(cms):
84100plot_examples ([viridis , newcmp ])
85101
86102##############################################################################
87- # and we can easily paste together two colormaps:
103+ # and we can easily concatenate two colormaps:
88104
89105top = cm .get_cmap ('Oranges_r' , 128 )
90106bottom = cm .get_cmap ('Blues' , 128 )
@@ -94,12 +110,25 @@ def plot_examples(cms):
94110newcmp = ListedColormap (newcolors , name = 'OrangeBlue' )
95111plot_examples ([viridis , newcmp ])
96112
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+
97126##############################################################################
98127# LinearSegmented colormaps
99128# =========================
100129#
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
103132# (RGBA).
104133#
105134# The format to specify these colormaps is a bit complicated to allow
@@ -142,8 +171,8 @@ def plot_linearmap(cdict):
142171# element of the first anchor and the first element of the second anchor.
143172
144173cdict ['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 ]]
147176plot_linearmap (cdict )
148177
149178
@@ -164,6 +193,4 @@ def plot_linearmap(cdict):
164193matplotlib .colors .LinearSegmentedColormap
165194matplotlib .colors .ListedColormap
166195matplotlib .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