4
4
********************************
5
5
6
6
Matplotlib has a number of built-in colormaps accessible via
7
- `.matplotlib.cm.get_cmap `. There are also external libraries like
7
+ `.matplotlib.colormaps `. There are also external libraries like
8
8
palettable_ that have many extra colormaps.
9
9
10
10
.. _palettable: https://jiffyclub.github.io/palettable/
24
24
============================================
25
25
26
26
First, getting a named colormap, most of which are listed in
27
- :doc:`/tutorials/colors/colormaps`, may be done using
28
- `.matplotlib.cm.get_cmap`, which returns a colormap object.
29
- The second argument gives the size of the list of colors used to define the
30
- colormap, and below we use a modest value of 8 so there are not a lot of
31
- values to look at.
27
+ :doc:`/tutorials/colors/colormaps`, may be done using `.matplotlib.colormaps`,
28
+ which returns a colormap object. The length of the list of colors used
29
+ internally to define the colormap and be adjusted via `.Colormap.resampled`.
30
+ Blow we use a modest value of 8 so there are not a lot of values to look at.
31
+
32
32
"""
33
33
34
34
import numpy as np
35
35
import matplotlib .pyplot as plt
36
- from matplotlib import cm
36
+ import matplotlib as mpl
37
37
from matplotlib .colors import ListedColormap , LinearSegmentedColormap
38
38
39
- viridis = cm . get_cmap ( 'viridis' , 8 )
39
+ viridis = mpl . colormaps [ 'viridis' ]. resampled ( 8 )
40
40
41
41
##############################################################################
42
42
# The object ``viridis`` is a callable, that when passed a float between
72
72
# However, one may still call the colormap with an integer array, or with a
73
73
# float array between 0 and 1.
74
74
75
- copper = cm . get_cmap ( 'copper' , 8 )
75
+ copper = mpl . colormaps [ 'copper' ]. resampled ( 8 )
76
76
77
77
print ('copper(range(8))' , copper (range (8 )))
78
78
print ('copper(np.linspace(0, 1, 8))' , copper (np .linspace (0 , 1 , 8 )))
@@ -123,7 +123,7 @@ def plot_examples(colormaps):
123
123
# For example, suppose we want to make the first 25 entries of a 256-length
124
124
# "viridis" colormap pink for some reason:
125
125
126
- viridis = cm . get_cmap ( 'viridis' , 256 )
126
+ viridis = mpl . colormaps [ 'viridis' ]. resampled ( 256 )
127
127
newcolors = viridis (np .linspace (0 , 1 , 256 ))
128
128
pink = np .array ([248 / 256 , 24 / 256 , 148 / 256 , 1 ])
129
129
newcolors [:25 , :] = pink
@@ -138,15 +138,15 @@ def plot_examples(colormaps):
138
138
# values that were in the original colormap. This method does not interpolate
139
139
# in color-space to add new colors.
140
140
141
- viridis_big = cm . get_cmap ( 'viridis' )
141
+ viridis_big = mpl . colormaps [ 'viridis' ]
142
142
newcmp = ListedColormap (viridis_big (np .linspace (0.25 , 0.75 , 128 )))
143
143
plot_examples ([viridis , newcmp ])
144
144
145
145
##############################################################################
146
146
# and we can easily concatenate two colormaps:
147
147
148
- top = cm . get_cmap ( 'Oranges_r' , 128 )
149
- bottom = cm . get_cmap ( 'Blues' , 128 )
148
+ top = mpl . colormaps [ 'Oranges_r' ]. resampled ( 128 )
149
+ bottom = mpl . colormaps [ 'Blues' ]. resampled ( 128 )
150
150
151
151
newcolors = np .vstack ((top (np .linspace (0 , 1 , 128 )),
152
152
bottom (np .linspace (0 , 1 , 128 ))))
@@ -268,4 +268,4 @@ def plot_linearmap(cdict):
268
268
# - `matplotlib.colors.LinearSegmentedColormap`
269
269
# - `matplotlib.colors.ListedColormap`
270
270
# - `matplotlib.cm`
271
- # - `matplotlib.cm.get_cmap `
271
+ # - `matplotlib.colormaps `
0 commit comments