Skip to content

Commit ceac93e

Browse files
committed
Use mpl.colormaps in examples
Also don't obtain the Colormap object if it only passed to a colormapping function. Passing the name is sufficient in such cases.
1 parent 67d04a8 commit ceac93e

File tree

10 files changed

+29
-34
lines changed

10 files changed

+29
-34
lines changed

examples/axes_grid1/demo_edge_colorbar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def demo_bottom_cbar(fig):
3535
)
3636

3737
Z, extent = get_demo_image()
38-
cmaps = [plt.get_cmap("autumn"), plt.get_cmap("summer")]
38+
cmaps = ["autumn", "summer"]
3939
for i in range(4):
4040
im = grid[i].imshow(Z, extent=extent, cmap=cmaps[i//2])
4141
if i % 2:
@@ -65,7 +65,7 @@ def demo_right_cbar(fig):
6565
cbar_pad="2%",
6666
)
6767
Z, extent = get_demo_image()
68-
cmaps = [plt.get_cmap("spring"), plt.get_cmap("winter")]
68+
cmaps = ["spring", "winter"]
6969
for i in range(4):
7070
im = grid[i].imshow(Z, extent=extent, cmap=cmaps[i//2])
7171
if i % 2:

examples/color/colormap_reference.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ def plot_color_gradients(cmap_category, cmap_list):
5454

5555
axs[0].set_title(cmap_category + ' colormaps', fontsize=14)
5656

57-
for ax, name in zip(axs, cmap_list):
58-
ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))
59-
ax.text(-.01, .5, name, va='center', ha='right', fontsize=10,
57+
for ax, cmap_name in zip(axs, cmap_list):
58+
ax.imshow(gradient, aspect='auto', cmap=cmap_name)
59+
ax.text(-.01, .5, cmap_name, va='center', ha='right', fontsize=10,
6060
transform=ax.transAxes)
6161

6262
# Turn off *all* ticks & spines, not just the ones with colormaps.

examples/color/custom_cmap.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
7070
"""
7171
import numpy as np
72+
import matplotlib as mpl
7273
import matplotlib.pyplot as plt
7374
from matplotlib.colors import LinearSegmentedColormap
7475

@@ -167,11 +168,9 @@
167168
# of Colormap, not just
168169
# a LinearSegmentedColormap:
169170

170-
blue_red2 = LinearSegmentedColormap('BlueRed2', cdict2)
171-
plt.register_cmap(cmap=blue_red2)
172-
173-
plt.register_cmap(cmap=LinearSegmentedColormap('BlueRed3', cdict3))
174-
plt.register_cmap(cmap=LinearSegmentedColormap('BlueRedAlpha', cdict4))
171+
mpl.colormaps.register(LinearSegmentedColormap('BlueRed2', cdict2))
172+
mpl.colormaps.register(LinearSegmentedColormap('BlueRed3', cdict3))
173+
mpl.colormaps.register(LinearSegmentedColormap('BlueRedAlpha', cdict4))
175174

176175
###############################################################################
177176
# Make the figure:
@@ -184,8 +183,7 @@
184183
im1 = axs[0, 0].imshow(Z, cmap=blue_red1)
185184
fig.colorbar(im1, ax=axs[0, 0])
186185

187-
cmap = plt.get_cmap('BlueRed2')
188-
im2 = axs[1, 0].imshow(Z, cmap=cmap)
186+
im2 = axs[1, 0].imshow(Z, cmap='BlueRed2')
189187
fig.colorbar(im2, ax=axs[1, 0])
190188

191189
# Now we will set the third cmap as the default. One would

examples/images_contours_and_fields/contourf_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
How to use the `.axes.Axes.contourf` method to create filled contour plots.
77
"""
88
import numpy as np
9+
import matplotlib as mpl
910
import matplotlib.pyplot as plt
1011

1112
origin = 'lower'
@@ -86,8 +87,7 @@
8687

8788
# Illustrate all 4 possible "extend" settings:
8889
extends = ["neither", "both", "min", "max"]
89-
cmap = plt.cm.get_cmap("winter")
90-
cmap = cmap.with_extremes(under="magenta", over="yellow")
90+
cmap = mpl.colormaps["winter"].with_extremes(under="magenta", over="yellow")
9191
# Note: contouring simply excludes masked or nan regions, so
9292
# instead of using the "bad" colormap value for them, it draws
9393
# nothing at all in them. Therefore the following would have

examples/images_contours_and_fields/demo_bboximage.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
a bounding box. This demo shows how to show an image inside a `.text.Text`'s
88
bounding box as well as how to manually create a bounding box for the image.
99
"""
10+
import matplotlib as mpl
1011
import matplotlib.pyplot as plt
1112
import numpy as np
1213
from matplotlib.image import BboxImage
@@ -38,18 +39,18 @@
3839
a = np.vstack((a, a))
3940

4041
# List of all colormaps; skip reversed colormaps.
41-
maps = sorted(m for m in plt.colormaps() if not m.endswith("_r"))
42+
cmap_names = sorted(m for m in mpl.colormaps if not m.endswith("_r"))
4243

4344
ncol = 2
44-
nrow = len(maps)//ncol + 1
45+
nrow = len(cmap_names) // ncol + 1
4546

4647
xpad_fraction = 0.3
47-
dx = 1./(ncol + xpad_fraction*(ncol - 1))
48+
dx = 1 / (ncol + xpad_fraction * (ncol - 1))
4849

4950
ypad_fraction = 0.3
50-
dy = 1./(nrow + ypad_fraction*(nrow - 1))
51+
dy = 1 / (nrow + ypad_fraction * (nrow - 1))
5152

52-
for i, m in enumerate(maps):
53+
for i, cmap_name in enumerate(cmap_names):
5354
ix, iy = divmod(i, nrow)
5455

5556
bbox0 = Bbox.from_bounds(ix*dx*(1 + xpad_fraction),
@@ -58,7 +59,7 @@
5859
bbox = TransformedBbox(bbox0, ax2.transAxes)
5960

6061
bbox_image = BboxImage(bbox,
61-
cmap=plt.get_cmap(m),
62+
cmap=cmap_name,
6263
norm=None,
6364
origin=None,
6465
**kwargs

examples/images_contours_and_fields/quadmesh_demo.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
This demo illustrates a bug in quadmesh with masked data.
1010
"""
1111

12-
from matplotlib import cm, pyplot as plt
12+
import matplotlib as mpl
13+
from matplotlib import pyplot as plt
1314
import numpy as np
1415

1516
n = 12
@@ -29,7 +30,7 @@
2930
axs[0].set_title('Without masked values')
3031

3132
# You can control the color of the masked region.
32-
cmap = cm.get_cmap(plt.rcParams['image.cmap']).with_extremes(bad='y')
33+
cmap = mpl.colormaps[mpl.rcParams['image.cmap']].with_extremes(bad='y')
3334
axs[1].pcolormesh(Qx, Qz, Zm, shading='gouraud', cmap=cmap)
3435
axs[1].set_title('With masked values')
3536

examples/images_contours_and_fields/tricontour_smooth_delaunay.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
"""
2626
from matplotlib.tri import Triangulation, TriAnalyzer, UniformTriRefiner
2727
import matplotlib.pyplot as plt
28-
import matplotlib.cm as cm
2928
import numpy as np
3029

3130

@@ -114,19 +113,18 @@ def experiment_res(x, y):
114113

115114
# Graphical options for tricontouring
116115
levels = np.arange(0., 1., 0.025)
117-
cmap = cm.get_cmap(name='Blues', lut=None)
118116

119117
fig, ax = plt.subplots()
120118
ax.set_aspect('equal')
121119
ax.set_title("Filtering a Delaunay mesh\n"
122120
"(application to high-resolution tricontouring)")
123121

124122
# 1) plot of the refined (computed) data contours:
125-
ax.tricontour(tri_refi, z_test_refi, levels=levels, cmap=cmap,
123+
ax.tricontour(tri_refi, z_test_refi, levels=levels, cmap='Blues',
126124
linewidths=[2.0, 0.5, 1.0, 0.5])
127125
# 2) plot of the expected (analytical) data contours (dashed):
128126
if plot_expected:
129-
ax.tricontour(tri_refi, z_expected, levels=levels, cmap=cmap,
127+
ax.tricontour(tri_refi, z_expected, levels=levels, cmap='Blues',
130128
linestyles='--')
131129
# 3) plot of the fine mesh on which interpolation was done:
132130
if plot_refi_tri:

examples/images_contours_and_fields/tricontour_smooth_user.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"""
99
import matplotlib.tri as tri
1010
import matplotlib.pyplot as plt
11-
import matplotlib.cm as cm
1211
import numpy as np
1312

1413

@@ -66,8 +65,7 @@ def function_z(x, y):
6665
ax.triplot(triang, lw=0.5, color='white')
6766

6867
levels = np.arange(0., 1., 0.025)
69-
cmap = cm.get_cmap(name='terrain', lut=None)
70-
ax.tricontourf(tri_refi, z_test_refi, levels=levels, cmap=cmap)
68+
ax.tricontourf(tri_refi, z_test_refi, levels=levels, cmap='terrain')
7169
ax.tricontour(tri_refi, z_test_refi, levels=levels,
7270
colors=['0.25', '0.5', '0.5', '0.5', '0.5'],
7371
linewidths=[1.0, 0.5, 0.5, 0.5, 0.5])

examples/images_contours_and_fields/trigradient_demo.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from matplotlib.tri import (
1010
Triangulation, UniformTriRefiner, CubicTriInterpolator)
1111
import matplotlib.pyplot as plt
12-
import matplotlib.cm as cm
1312
import numpy as np
1413

1514

@@ -76,8 +75,7 @@ def dipole_potential(x, y):
7675
ax.triplot(triang, color='0.8')
7776

7877
levels = np.arange(0., 1., 0.01)
79-
cmap = cm.get_cmap(name='hot', lut=None)
80-
ax.tricontour(tri_refi, z_test_refi, levels=levels, cmap=cmap,
78+
ax.tricontour(tri_refi, z_test_refi, levels=levels, cmap='hot',
8179
linewidths=[2.0, 1.0, 1.0, 1.0])
8280
# Plots direction of the electrical vector field
8381
ax.quiver(triang.x, triang.y, Ex/E_norm, Ey/E_norm,

examples/pie_and_polar_charts/nested_pie.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
99
"""
1010

11+
import matplotlib as mpl
1112
import matplotlib.pyplot as plt
1213
import numpy as np
1314

@@ -30,7 +31,7 @@
3031
size = 0.3
3132
vals = np.array([[60., 32.], [37., 40.], [29., 10.]])
3233

33-
cmap = plt.get_cmap("tab20c")
34+
cmap = mpl.colormaps["tab20c"]
3435
outer_colors = cmap(np.arange(3)*4)
3536
inner_colors = cmap([1, 2, 5, 6, 9, 10])
3637

@@ -61,7 +62,7 @@
6162
# Obtain the ordinates of the bar edges
6263
valsleft = np.cumsum(np.append(0, valsnorm.flatten()[:-1])).reshape(vals.shape)
6364

64-
cmap = plt.get_cmap("tab20c")
65+
cmap = mpl.colormaps["tab20c"]
6566
outer_colors = cmap(np.arange(3)*4)
6667
inner_colors = cmap([1, 2, 5, 6, 9, 10])
6768

0 commit comments

Comments
 (0)