Skip to content

Commit bf6bb85

Browse files
committed
DOC: remove cm.get_cmap from examples and tutorials
1 parent a17f4f3 commit bf6bb85

File tree

5 files changed

+23
-23
lines changed

5 files changed

+23
-23
lines changed

examples/images_contours_and_fields/contour_image.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
axs = _axs.flatten()
4242

4343
cset1 = axs[0].contourf(X, Y, Z, levels, norm=norm,
44-
cmap=cm.get_cmap(cmap, len(levels) - 1))
44+
cmap=cmap.resampled(len(levels) - 1))
4545
# It is not necessary, but for the colormap, we need only the
4646
# number of levels minus 1. To avoid discretization error, use
4747
# either this number or a large number such as the default (256).

examples/images_contours_and_fields/image_annotated_heatmap.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
import numpy as np
4141
import matplotlib
42+
import matplotlib as mpl
4243
import matplotlib.pyplot as plt
4344
# sphinx_gallery_thumbnail_number = 2
4445

@@ -272,7 +273,7 @@ def annotate_heatmap(im, data=None, valfmt="{x:.2f}",
272273
fmt = matplotlib.ticker.FuncFormatter(lambda x, pos: qrates[::-1][norm(x)])
273274

274275
im, _ = heatmap(data, y, x, ax=ax3,
275-
cmap=plt.get_cmap("PiYG", 7), norm=norm,
276+
cmap=mpl.colormaps["PiYG"].resampled(7), norm=norm,
276277
cbar_kw=dict(ticks=np.arange(-3, 4), format=fmt),
277278
cbarlabel="Quality Rating")
278279

examples/lines_bars_and_markers/multivariate_marker_plot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
positions = np.random.normal(size=(N, 2)) * 5
3232
data = zip(skills, takeoff_angles, thrusts, successful, positions)
3333

34-
cmap = plt.cm.get_cmap("plasma")
34+
cmap = plt.colormaps["plasma"]
3535
fig, ax = plt.subplots()
3636
fig.suptitle("Throwing success", size=14)
3737
for skill, takeoff, thrust, mood, pos in data:

tutorials/colors/colormap-manipulation.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
********************************
55
66
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
88
palettable_ that have many extra colormaps.
99
1010
.. _palettable: https://jiffyclub.github.io/palettable/
@@ -24,19 +24,19 @@
2424
============================================
2525
2626
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+
3232
"""
3333

3434
import numpy as np
3535
import matplotlib.pyplot as plt
36-
from matplotlib import cm
36+
import matplotlib as mpl
3737
from 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
@@ -72,7 +72,7 @@
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

7777
print('copper(range(8))', copper(range(8)))
7878
print('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)
127127
newcolors = viridis(np.linspace(0, 1, 256))
128128
pink = np.array([248/256, 24/256, 148/256, 1])
129129
newcolors[: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']
142142
newcmp = ListedColormap(viridis_big(np.linspace(0.25, 0.75, 128)))
143143
plot_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

151151
newcolors = 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`

tutorials/colors/colormaps.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
********************************
55
66
Matplotlib has a number of built-in colormaps accessible via
7-
`.matplotlib.cm.get_cmap`. There are also external libraries that
7+
`.matplotlib.colormaps`. There are also external libraries that
88
have many extra colormaps, which can be viewed in the
99
`Third-party colormaps`_ section of the Matplotlib documentation.
1010
Here we briefly discuss how to choose between the many options. For
@@ -80,7 +80,6 @@
8080
import numpy as np
8181
import matplotlib as mpl
8282
import matplotlib.pyplot as plt
83-
from matplotlib import cm
8483
from colorspacious import cspace_converter
8584

8685

@@ -105,7 +104,7 @@ def plot_color_gradients(category, cmap_list):
105104
axs[0].set_title(f'{category} colormaps', fontsize=14)
106105

107106
for ax, name in zip(axs, cmap_list):
108-
ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))
107+
ax.imshow(gradient, aspect='auto', cmap=mpl.colormaps[name])
109108
ax.text(-0.01, 0.5, name, va='center', ha='right', fontsize=10,
110109
transform=ax.transAxes)
111110

@@ -277,7 +276,7 @@ def plot_color_gradients(category, cmap_list):
277276

278277
# Get RGB values for colormap and convert the colormap in
279278
# CAM02-UCS colorspace. lab[0, :, 0] is the lightness.
280-
rgb = cm.get_cmap(cmap)(x)[np.newaxis, :, :3]
279+
rgb = mpl.colormaps[cmap](x)[np.newaxis, :, :3]
281280
lab = cspace_converter("sRGB1", "CAM02-UCS")(rgb)
282281

283282
# Plot colormap L values. Do separately for each category
@@ -380,14 +379,14 @@ def plot_color_gradients(cmap_category, cmap_list):
380379
for ax, name in zip(axs, cmap_list):
381380

382381
# Get RGB values for colormap.
383-
rgb = cm.get_cmap(plt.get_cmap(name))(x)[np.newaxis, :, :3]
382+
rgb = mpl.colormaps[name](x)[np.newaxis, :, :3]
384383

385384
# Get colormap in CAM02-UCS colorspace. We want the lightness.
386385
lab = cspace_converter("sRGB1", "CAM02-UCS")(rgb)
387386
L = lab[0, :, 0]
388387
L = np.float32(np.vstack((L, L, L)))
389388

390-
ax[0].imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))
389+
ax[0].imshow(gradient, aspect='auto', cmap=mpl.colormaps[name])
391390
ax[1].imshow(L, aspect='auto', cmap='binary_r', vmin=0., vmax=100.)
392391
pos = list(ax[0].get_position().bounds)
393392
x_text = pos[0] - 0.01

0 commit comments

Comments
 (0)