A colormap in Matplotlib is a mapping from numeric values to colors, used to encode a third variable visually — beyond position on the x and y axes. Colormaps appear most often in:
- Scatter plots where points are colored by class or by a numeric attribute.
- Heat maps where the value of each cell is encoded as color.
- Contour plots, surface plots, and other 2D-to-3D-information renderings.
Matplotlib ships with several families. The right family depends on what the underlying data is:
Qualitative colormaps — Pastel1, Pastel2, Paired, Accent, Dark2, Set1, Set2, Set3, tab10, tab20, tab20b, tab20c. These are for finite, unordered categories. The third color in tab10 isn’t bigger than the second, just different. Use them to color a few-dozen classes in a scatter plot, with one color per class.
Sequential colormaps — viridis, plasma, magma, inferno, cividis, Blues, Reds, and many more. These are for ordered, continuous values. Colors run along a perceptual gradient — usually dark-to-light or saturated-to-pale — so the visual difference between two colors corresponds to the numeric difference between their values. viridis is the modern default because it’s perceptually uniform (equal numeric differences look equally different) and works for color-blind viewers.
Diverging colormaps — RdBu, coolwarm, seismic, BrBG. These are for values that have a meaningful midpoint — positive vs. negative correlations, deviations above and below a baseline. Two distinct hues on opposite sides of a neutral center.
Setting a colormap in a plotting call:
ax.scatter(x, y, c=values, cmap='viridis')
ax.imshow(matrix, cmap='RdBu_r', vmin=-1, vmax=1)The _r suffix reverses the colormap. vmin and vmax set the data range that the colormap covers; values outside the range get clipped to the endpoints.
norm is for advanced cases where we want a non-linear mapping from data to colors — log scales, for instance, via LogNorm().
Picking an appropriate colormap is part of the art of effective visualization. Using a qualitative colormap on continuous data, or a sequential colormap on categorical data, sends the wrong visual signal to the reader.