8000 Update colormap usage documentation to prioritize string colormap names by Kaustbh · Pull Request #29028 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Update colormap usage documentation to prioritize string colormap names #29028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions doc/users/prev_whats_new/whats_new_3.10.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ colour maps version 8.0.1 (DOI: https://doi.org/10.5281/zenodo.1243862).
img = np.sin(x*y)

_, ax = plt.subplots(1, 3)
ax[0].imshow(img, cmap=plt.cm.berlin)
ax[1].imshow(img, cmap=plt.cm.managua)
ax[2].imshow(img, cmap=plt.cm.vanimo)
ax[0].imshow(img, cmap="berlin")
ax[1].imshow(img, cmap="managua")
ax[2].imshow(img, cmap="vanimo")



Expand Down
2 changes: 1 addition & 1 deletion galleries/examples/axisartist/demo_curvelinear_grid2.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def inv_tr(x, y):
# itself (i.e., transData) is not affected by the given transform.

ax1.imshow(np.arange(25).reshape(5, 5),
vmax=50, cmap=plt.cm.gray_r, origin="lower")
vmax=50, cmap="gray_r", origin="lower")


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import matplotlib.pyplot as plt
import numpy as np

import matplotlib.cm as cm

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
Expand Down Expand Up @@ -79,7 +77,7 @@

fig, ax = plt.subplots()
im = ax.imshow(Z, interpolation='bilinear', origin='lower',
cmap=cm.gray, extent=(-3, 3, -2, 2))
cmap="gray", extent=(-3, 3, -2, 2))
levels = np.arange(-1.2, 1.6, 0.2)
CS = ax.contour(Z, levels, origin='lower', cmap='flag', extend='both',
linewidths=2, extent=(-3, 3, -2, 2))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
levels = np.arange(-2.0, 1.601, 0.4)

norm = cm.colors.Normalize(vmax=abs(Z).max(), vmin=-abs(Z).max())
cmap = cm.PRGn
cmap = plt.colormaps["PRGn"]

fig, _axs = plt.subplots(nrows=2, ncols=2)
fig.subplots_adjust(hspace=0.3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
# for purposes of illustration.

fig1, ax2 = plt.subplots(layout='constrained')
CS = ax2.contourf(X, Y, Z, 10, cmap=plt.cm.bone)
CS = ax2.contourf(X, Y, Z, 10, cmap="bone")

# Note that in the following, we explicitly pass in a subset of the contour
# levels used for the filled contours. Alternatively, we could pass in
Expand Down
4 changes: 2 additions & 2 deletions galleries/examples/images_contours_and_fields/contourf_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import numpy as np
from numpy import ma

from matplotlib import cm, ticker
from matplotlib import ticker

N = 100
x = np.linspace(-3.0, 3.0, N)
Expand All @@ -36,7 +36,7 @@
# Automatic selection of levels works; setting the
# log locator tells contourf to use a log scale:
fig, ax = plt.subplots()
cs = ax.contourf(X, Y, z, locator=ticker.LogLocator(), cmap=cm.PuBu_r)
cs = ax.contourf(X, Y, z, locator=ticker.LogLocator(), cmap="PuBu_r")

# Alternatively, you can manually set the levels
# and the norm:
Expand Down
7 changes: 3 additions & 4 deletions galleries/examples/images_contours_and_fields/image_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import numpy as np

import matplotlib.cbook as cbook
import matplotlib.cm as cm
from matplotlib.patches import PathPatch
from matplotlib.path import Path

Expand All @@ -30,7 +29,7 @@
Z = (Z1 - Z2) * 2

fig, ax = plt.subplots()
im = ax.imshow(Z, interpolation='bilinear', cmap=cm.RdYlBu,
im = ax.imshow(Z, interpolation='bilinear', cmap="RdYlBu",
origin='lower', extent=[-3, 3, -3, 3],
vmax=abs(Z).max(), vmin=-abs(Z).max())

Expand Down Expand Up @@ -58,7 +57,7 @@
ax['hopper'].imshow(image)
ax['hopper'].axis('off') # clear x-axis and y-axis

im = ax['mri'].imshow(A, cmap=plt.cm.hot, origin='upper', extent=extent)
im = ax['mri'].imshow(A, cmap="hot", origin='upper', extent=extent)

markers = [(15.9, 14.5), (16.8, 15)]
x, y = zip(*markers)
Expand Down Expand Up @@ -163,7 +162,7 @@
fig, ax = plt.subplots()
ax.add_patch(patch)

im = ax.imshow(Z, interpolation='bilinear', cmap=cm.gray,
im = ax.imshow(Z, interpolation='bilinear', cmap="gray",
origin='lower', extent=[-3, 3, -3, 3],
clip_path=patch, clip_on=True)
im.set_clip_path(patch)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
Z = (Z1 - Z2) * 2

# Set up a colormap:
palette = plt.cm.gray.with_extremes(over='r', under='g', bad='b')
palette = plt.colormaps["gray"].with_extremes(over='r', under='g', bad='b')
# Alternatively, we could use
# palette.set_bad(alpha = 0.0)
# to make the bad region transparent. This is the default.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import matplotlib.pyplot as plt
import numpy as np

from matplotlib import cm
from matplotlib.image import NonUniformImage

interp = 'nearest'
Expand All @@ -30,7 +29,7 @@
fig.suptitle('NonUniformImage class', fontsize='large')
ax = axs[0, 0]
im = NonUniformImage(ax, interpolation=interp, extent=(-4, 4, -4, 4),
cmap=cm.Purples)
cmap="Purples")
im.set_data(x, y, z)
ax.add_image(im)
ax.set_xlim(-4, 4)
Expand All @@ -39,7 +38,7 @@

ax = axs[0, 1]
im = NonUniformImage(ax, interpolation=interp, extent=(-64, 64, -4, 4),
cmap=cm.Purples)
cmap="Purples")
im.set_data(x2, y, z)
ax.add_image(im)
ax.set_xlim(-64, 64)
Expand All @@ -50,7 +49,7 @@

ax = axs[1, 0]
im = NonUniformImage(ax, interpolation=interp, extent=(-4, 4, -4, 4),
cmap=cm.Purples)
cmap="Purples")
im.set_data(x, y, z)
ax.add_image(im)
ax.set_xlim(-4, 4)
Expand All @@ -59,7 +58,7 @@

ax = axs[1, 1]
im = NonUniformImage(ax, interpolation=interp, extent=(-64, 64, -4, 4),
cmap=cm.Purples)
cmap="Purples")
im.set_data(x2, y, z)
ax.add_image(im)
ax.set_xlim(-64, 64)
Expand Down
4 changes: 2 additions & 2 deletions galleries/examples/images_contours_and_fields/layer_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ def func3(x, y):
fig = plt.figure(frameon=False)

Z1 = np.add.outer(range(8), range(8)) % 2 # chessboard
im1 = plt.imshow(Z1, cmap=plt.cm.gray, interpolation='nearest',
im1 = plt.imshow(Z1, cmap="gray", interpolation='nearest',
extent=extent)

Z2 = func3(X, Y)

im2 = plt.imshow(Z2, cmap=plt.cm.viridis, alpha=.9, interpolation='bilinear',
im2 = plt.imshow(Z2, cmap="viridis", alpha=.9, interpolation='bilinear',
extent=extent)

plt.show()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ def main():
dem = cbook.get_sample_data('jacksboro_fault_dem.npz')
elev = dem['elevation']

fig = compare(z, plt.cm.copper)
fig = compare(z, plt.colormaps["copper"])
fig.suptitle('HSV Blending Looks Best with Smooth Surfaces', y=0.95)

fig = compare(elev, plt.cm.gist_earth, ve=0.05)
fig = compare(elev, plt.colormaps["gist_earth"], ve=0.05)
fig.suptitle('Overlay Blending Looks Best with Rough Surfaces', y=0.95)

plt.show()
Expand Down
4 changes: 2 additions & 2 deletions galleries/examples/lines_bars_and_markers/gradient_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ def gradient_bar(ax, x, y, width=0.5, bottom=0):
for left, top in zip(x, y):
right = left + width
gradient_image(ax, extent=(left, right, bottom, top),
cmap=plt.cm.Blues_r, cmap_range=(0, 0.8))
cmap="Blues_r", cmap_range=(0, 0.8))


fig, ax = plt.subplots()
ax.set(xlim=(0, 10), ylim=(0, 1))

# background image
gradient_image(ax, direction=1, extent=(0, 1, 0, 1), transform=ax.transAxes,
cmap=plt.cm.RdYlGn, cmap_range=(0.2, 0.8), alpha=0.5)
cmap="RdYlGn", cmap_range=(0.2, 0.8), alpha=0.5)

N = 10
x = np.arange(N) + 0.15
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
t = Affine2D().scale(skill).rotate_deg(takeoff)
m = MarkerStyle(SUCCESS_SYMBOLS[mood], transform=t)
ax.plot(pos[0], pos[1], marker=m, color=cmap(thrust))
fig.colorbar(plt.cm.ScalarMappable(norm=Normalize(0, 1), cmap=cmap),
fig.colorbar(plt.cm.ScalarMappable(norm=Normalize(0, 1), cmap="plasma"),
ax=ax, label="Normalized Thrust [a.u.]")
ax.set_xlabel("X position [m]")
ax.set_ylabel("Y position [m]")
Expand Down
5 changes: 2 additions & 3 deletions galleries/examples/misc/contour_manual.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import matplotlib.pyplot as plt

import matplotlib.cm as cm
from matplotlib.contour import ContourSet
from matplotlib.path import Path

Expand All @@ -30,12 +29,12 @@
fig, ax = plt.subplots()

# Filled contours using filled=True.
cs = ContourSet(ax, [0, 1, 2], [filled01, filled12], filled=True, cmap=cm.bone)
cs = ContourSet(ax, [0, 1, 2], [filled01, filled12], filled=True, cmap="bone")
cbar = fig.colorbar(cs)

# Contour lines (non-filled).
lines = ContourSet(
ax, [0, 1, 2], [lines0, lines1, lines2], cmap=cm.cool, linewidths=3)
ax, [0, 1, 2], [lines0, lines1, lines2], cmap="cool", linewidths=3)
cbar.add_lines(lines)

ax.set(xlim=(-0.5, 3.5), ylim=(-0.5, 4.5),
Expand Down
3 changes: 1 addition & 2 deletions galleries/examples/misc/demo_agg_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import numpy as np

from matplotlib.artist import Artist
import matplotlib.cm as cm
from matplotlib.colors import LightSource
import matplotlib.transforms as mtransforms

Expand Down Expand Up @@ -179,7 +178,7 @@ def filtered_text(ax):

# draw
ax.imshow(Z, interpolation='bilinear', origin='lower',
cmap=cm.gray, extent=(-3, 3, -2, 2), aspect='auto')
cmap="gray", extent=(-3, 3, -2, 2), aspect='auto')
levels = np.arange(-1.2, 1.6, 0.2)
CS = ax.contour(Z, levels,
origin='lower',
Expand Down
4 changes: 1 addition & 3 deletions galleries/examples/misc/hyperlinks_sgskip.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import matplotlib.pyplot as plt
import numpy as np

import matplotlib.cm as cm

# %%

fig = plt.figure()
Expand All @@ -32,7 +30,7 @@
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2

im = plt.imshow(Z, interpolation='bilinear', cmap=cm.gray,
im = plt.imshow(Z, interpolation='bilinear', cmap="gray",
origin='lower', extent=(-3, 3, -3, 3))

im.set_url('https://www.google.com/')
Expand Down
2 changes: 1 addition & 1 deletion galleries/examples/misc/table_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
value_increment = 1000

# Get some pastel shades for the colors
colors = plt.cm.BuPu(np.linspace(0, 0.5, len(rows)))
colors = plt.colormaps["BuPu"](np.linspace(0, 0.5, len(rows)))
n_rows = len(data)

index = np.arange(len(columns)) + 0.3
Expand Down
3 changes: 1 addition & 2 deletions galleries/examples/mplot3d/contour3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@

import matplotlib.pyplot as plt

from matplotlib import cm
from mpl_toolkits.mplot3d import axes3d

ax = plt.figure().add_subplot(projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)

ax.contour(X, Y, Z, cmap=cm.coolwarm) # Plot contour curves
ax.contour(X, Y, Z, cmap="coolwarm") # Plot contour curves

plt.show()

Expand Down
3 changes: 1 addition & 2 deletions galleries/examples/mplot3d/contour3d_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@

import matplotlib.pyplot as plt

from matplotlib import cm
from mpl_toolkits.mplot3d import axes3d

ax = plt.figure().add_subplot(projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
ax.contour(X, Y, Z, extend3d=True, cmap=cm.coolwarm)
ax.contour(X, Y, Z, extend3d=True, cmap="coolwarm")

plt.show()

Expand Down
3 changes: 1 addition & 2 deletions galleries/examples/mplot3d/contourf3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@

import matplotlib.pyplot as plt

from matplotlib import cm
from mpl_toolkits.mplot3d import axes3d

ax = plt.figure().add_subplot(projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
ax.contourf(X, Y, Z, cmap=cm.coolwarm)
ax.contourf(X, Y, Z, cmap="coolwarm")

plt.show()

Expand Down
4 changes: 2 additions & 2 deletions galleries/examples/mplot3d/custom_shaded_3d_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import matplotlib.pyplot as plt
import numpy as np

from matplotlib import cbook, cm
from matplotlib import cbook
from matplotlib.colors import LightSource

# Load and format data
Expand All @@ -29,7 +29,7 @@
ls = LightSource(270, 45)
# To use a custom hillshading mode, override the built-in shading and pass
# in the rgb colors of the shaded surface calculated from "shade".
rgb = ls.shade(z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode='soft')
rgb = ls.shade(z, cmap=plt.colormaps["gist_earth"], vert_exag=0.1, blend_mode='soft')
surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=rgb,
linewidth=0, antialiased=False, shade=False)

Expand Down
3 changes: 1 addition & 2 deletions galleries/examples/mplot3d/subplot3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import matplotlib.pyplot as plt
import numpy as np

from matplotlib import cm
from mpl_toolkits.mplot3d.axes3d import get_test_data

# set up a figure twice as wide as it is tall
Expand All @@ -27,7 +26,7 @@
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap="coolwarm",
linewidth=0, antialiased=False)
ax.set_zlim(-1.01, 1.01)
fig.colorbar(surf, shrink=0.5, aspect=10)
Expand Down
3 changes: 1 addition & 2 deletions galleries/examples/mplot3d/surface3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import matplotlib.pyplot as plt
import numpy as np

from matplotlib import cm
from matplotlib.ticker import LinearLocator

fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
Expand All @@ -26,7 +25,7 @@
Z = np.sin(R)

# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
surf = ax.plot_surface(X, Y, Z, cmap="coolwarm",
linewidth=0, antialiased=False)

# Customize the z axis.
Expand Down
2 changes: 1 addition & 1 deletion galleries/examples/mplot3d/surface3d_radial.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
X, Y = R*np.cos(P), R*np.sin(P)

# Plot the surface.
ax.plot_surface(X, Y, Z, cmap=plt.cm.YlGnBu_r)
ax.plot_surface(X, Y, Z, cmap="YlGnBu_r")

# Tweak the limits and add latex math labels.
ax.set_zlim(0, 1)
Expand Down
2 changes: 1 addition & 1 deletion galleries/examples/mplot3d/tricontour3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
< min_radius)

ax = plt.figure().add_subplot(projection='3d')
ax.tricontour(triang, z, cmap=plt.cm.CMRmap)
ax.tricontour(triang, z, cmap="CMRmap")

# Customize the view angle so it's easier to understand the plot.
ax.view_init(elev=45.)
Expand Down
Loading
Loading
0