Matplotlib Colors & Colormaps

🌈 Using a colormap

scatter = ax.scatter(x, y, c=values, cmap="viridis")
fig.colorbar(scatter, ax=ax, label="Value")
 
img = ax.imshow(matrix, cmap="coolwarm")
fig.colorbar(img, ax=ax)

fig.colorbar() needs the actual plot object returned by scatter/imshow/etc β€” not the axes. Always capture the return value.

🎨 Common colormap categories

TypeUse forExamples
SequentialOrdered data, low β†’ high"viridis", "plasma", "Blues", "YlOrRd"
DivergingData with a meaningful midpoint (e.g. 0)"coolwarm", "RdBu", "seismic"
QualitativeDistinct, unordered categories"tab10", "Set2", "Pastel1"
CyclicData that wraps around (e.g. angles)"twilight", "hsv"

"viridis" is the modern default β€” perceptually uniform and colorblind-friendly. Avoid "jet" (old default, visually misleading).

print(plt.colormaps())     # list every available colormap name

πŸ”„ Reversed colormap

ax.imshow(matrix, cmap="viridis_r")   # append _r to reverse any colormap

🎚️ Normalization (controlling the color scale range)

import matplotlib.colors as mcolors
 
norm = mcolors.Normalize(vmin=0, vmax=100)
ax.scatter(x, y, c=values, cmap="viridis", norm=norm)
 
# diverging data centered at 0
norm = mcolors.TwoSlopeNorm(vmin=-10, vcenter=0, vmax=10)
ax.imshow(matrix, cmap="coolwarm", norm=norm)
 
# log-scaled color values
norm = mcolors.LogNorm(vmin=1, vmax=1000)

πŸ“Š Discrete colormap (fixed number of bins)

cmap = plt.get_cmap("viridis", 5)      # 5 discrete color levels instead of continuous

🎨 Manual color cycle

ax.set_prop_cycle(color=["red", "green", "blue"])
for y in datasets:
    ax.plot(x, y)      # cycles through the specified colors in order

πŸ–ŒοΈ Custom colormap

from matplotlib.colors import LinearSegmentedColormap
 
custom = LinearSegmentedColormap.from_list("custom", ["blue", "white", "red"])
ax.imshow(matrix, cmap=custom)

πŸŽ›οΈ Colorbar customization

cbar = fig.colorbar(img, ax=ax, label="Value", shrink=0.8, orientation="vertical")
cbar.set_ticks([0, 50, 100])
cbar.ax.set_ylabel("Value", rotation=270, labelpad=15)

πŸ”— Next

Matplotlib Plot Types Β· Matplotlib Saving & Exporting