Colormaps turn scalar values into an ordered color scale, which is what makes a heatmap, image, contour, or colored scatter plot readable. In Matplotlib, the colormap belongs to the plotted scalar mappable, so the choice should follow the data shape instead of the figure as a whole.

Pass a registered colormap name or object through the plotting call's cmap argument when creating scalar plots. The current registry is available through matplotlib.colormaps, and Matplotlib image and scatter defaults use rcParams[“image.cmap”] when no cmap is supplied.

Sequential maps such as viridis fit ordered measurements, while diverging maps fit values around a meaningful center. Direct plot colors are different; they set one fixed color for a line, marker, or bar rather than mapping many numeric values across a scale.

Steps to set a Matplotlib colormap:

  1. Choose a colormap family that matches the numeric meaning.

    Use sequential colormaps such as viridis for low-to-high measurements, diverging colormaps such as coolwarm for values around a baseline, and cyclic colormaps such as twilight for values that wrap around. Avoid qualitative maps for ordered scalar magnitudes.

  2. Save the colormap example as colormap_set.py.
    colormap_set.py
    from pathlib import Path
     
    import matplotlib
    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib import colormaps
     
    temperatures = np.array(
        [
            [18, 19, 21, 24, 26, 27, 25],
            [16, 18, 20, 22, 24, 25, 23],
            [21, 22, 24, 27, 29, 30, 28],
            [15, 17, 19, 21, 23, 24, 22],
        ]
    )
     
    stations = ["North", "West", "Central", "South"]
    days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
    output = Path("colormap-set.png")
     
    cmap = colormaps["viridis"]
     
    fig, ax = plt.subplots(figsize=(6.4, 3.8), layout="constrained")
    image = ax.imshow(temperatures, cmap=cmap, vmin=15, vmax=31)
    ax.set_xticks(range(len(days)), days)
    ax.set_yticks(range(len(stations)), stations)
    ax.set_xlabel("Day")
    ax.set_title("Weekly station temperature")
    colorbar = fig.colorbar(image, ax=ax, label="Temperature (deg C)")
     
    fig.savefig(output, dpi=160)
     
    print(f"matplotlib {matplotlib.__version__}")
    print(f"colormap: {image.get_cmap().name}")
    print(f"colorbar label: {colorbar.ax.get_ylabel()}")
    print(f"saved: {output}")

    colormaps[“viridis”] returns a registered colormap object. Passing cmap=“viridis” directly to imshow() or scatter() is also valid when no reuse is needed.

  3. Run the script from the directory where the PNG should be written.
    $ python colormap_set.py
    matplotlib 3.11.0
    colormap: viridis
    colorbar label: Temperature (deg C)
    saved: colormap-set.png
  4. Open colormap-set.png and confirm the colorbar matches the plot.

    The printed colormap: viridis and the colorbar label confirm that the saved image is using the selected colormap and scale. If a plotted object already exists, change that object explicitly with image.set_cmap(“magma”); plt.set_cmap() changes the default colormap and the current image if one exists.