44********************************
55
66Matplotlib 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
88palettable_ that have many extra colormaps.
99
1010.. _palettable: https://jiffyclub.github.io/palettable/
2424============================================
2525
2626First, 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+
3232"""
3333
3434import numpy as np
3535import matplotlib .pyplot as plt
36- from matplotlib import cm
36+ import matplotlib as mpl
3737from matplotlib .colors import ListedColormap , LinearSegmentedColormap
3838
39- viridis = cm . get_cmap ( 'viridis' , 8 )
39+ viridis = mpl . colormaps [ 'viridis' ]. resampled ( 8 )
4040
4141##############################################################################
4242# The object ``viridis`` is a callable, that when passed a float between
7272# However, one may still call the colormap with an integer array, or with a
7373# float array between 0 and 1.
7474
75- copper = cm . get_cmap ( 'copper' , 8 )
75+ copper = mpl . colormaps [ 'copper' ]. resampled ( 8 )
7676
7777print ('copper(range(8))' , copper (range (8 )))
7878print ('copper(np.linspace(0, 1, 8))' , copper (np .linspace (0 , 1 , 8 )))
@@ -123,7 +123,7 @@ def plot_examples(colormaps):
123123# For example, suppose we want to make the first 25 entries of a 256-length
124124# "viridis" colormap pink for some reason:
125125
126- viridis = cm . get_cmap ( 'viridis' , 256 )
126+ viridis = mpl . colormaps [ 'viridis' ]. resampled ( 256 )
127127newcolors = viridis (np .linspace (0 , 1 , 256 ))
128128pink = np .array ([248 / 256 , 24 / 256 , 148 / 256 , 1 ])
129129newcolors [:25 , :] = pink
@@ -138,15 +138,15 @@ def plot_examples(colormaps):
138138# values that were in the original colormap. This method does not interpolate
139139# in color-space to add new colors.
140140
141- viridis_big = cm . get_cmap ( 'viridis' )
141+ viridis_big = mpl . colormaps [ 'viridis' ]
142142newcmp = ListedColormap (viridis_big (np .linspace (0.25 , 0.75 , 128 )))
143143plot_examples ([viridis , newcmp ])
144144
145145##############################################################################
146146# and we can easily concatenate two colormaps:
147147
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 )
150150
151151newcolors = np .vstack ((top (np .linspace (0 , 1 , 128 )),
152152 bottom (np .linspace (0 , 1 , 128 ))))
@@ -268,4 +268,4 @@ def plot_linearmap(cdict):
268268# - `matplotlib.colors.LinearSegmentedColormap`
269269# - `matplotlib.colors.ListedColormap`
270270# - `matplotlib.cm`
271- # - `matplotlib.cm.get_cmap `
271+ # - `matplotlib.colormaps `
0 commit comments