Matplotlib reference

Matplotlib Colors and Colormaps

Every named color with its hex code, the default color cycle, and the common colormaps with their real colors from Matplotlib 3.11.2. Select a color or a colormap to copy its name, then try it in the examples, which run online with nothing to install.

How to set a color in Matplotlib

Any function that draws something takes a color argument (c for short in plot and scatter). Matplotlib understands all of these:

FormatExampleNotes
Named colorcolor="tomato"Any of the 148 CSS names below.
Hex codecolor="#1f77b4"Add two digits for transparency: "#1f77b480".
Tableau colorcolor="tab:orange"The ten colors of the default cycle.
Cycle colorcolor="C2"The third color of the current cycle (C0 is the first).
One lettercolor="r" or "r--"b g r c m y k w; also works in format strings.
RGB or RGBA tuplecolor=(0.1, 0.4, 0.8, 0.5)Values from 0 to 1, not 0 to 255.
Gray levelcolor="0.75"A string from "0" (black) to "1" (white).
colors.py
Run this chart →
colors.py
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
fig, ax = plt.subplots(figsize=(8, 5))

# Six ways to give a color
ax.plot(x, np.sin(x), color="tomato", label='"tomato" (named color)')
ax.plot(x, np.sin(x) + 1, color="#2a9d8f", label='"#2a9d8f" (hex code)')
ax.plot(x, np.sin(x) + 2, color="tab:purple", label='"tab:purple" (Tableau)')
ax.plot(x, np.sin(x) + 3, color=(0.9, 0.6, 0.1), label="(0.9, 0.6, 0.1) (RGB tuple)")
ax.plot(x, np.sin(x) + 4, color="C0", label='"C0" (first cycle color)')
ax.plot(x, np.sin(x) + 5, color="0.4", label='"0.4" (gray level)')

ax.legend(loc="upper right", fontsize=8)
ax.set_title("Ways to set a color in Matplotlib")
plt.show()

Default color cycle (Tableau colors)

When you do not choose a color, each new line or bar set takes the next of these ten colors. Use them by name ("tab:blue") or by position ("C0" to "C9").

Single-letter colors

Named colors (CSS4)

All 148 names Matplotlib accepts, grouped by hue. Select one to copy its name. "grey" and "gray" spellings both work.

Matplotlib colormaps (cmap)

A colormap turns numbers into colors in scatter, imshow, pcolormesh, contourf and heatmaps. Pass its name as cmap="viridis", and add _r for the reversed map ("viridis_r"). Select a colormap to copy its name.

Perceptually uniform sequential

Sequential

Diverging

Cyclic

Qualitative

Miscellaneous

colormaps.py
Run this chart →
colormaps.py
import matplotlib.pyplot as plt
import numpy as np

rng = np.random.default_rng(1)
x, y = rng.normal(size=(2, 400))
distance = np.hypot(x, y)

fig, (left, right) = plt.subplots(1, 2, figsize=(10, 4))

# A colormap turns numbers into colors; the colorbar is its legend.
points = left.scatter(x, y, c=distance, cmap="viridis", s=18)
fig.colorbar(points, ax=left, label="distance from center")
left.set_title('cmap="viridis"')

# A diverging map for data above and below zero
grid = np.sin(np.linspace(-3, 3, 60))[:, None] * np.cos(np.linspace(-3, 3, 60))[None, :]
image = right.imshow(grid, cmap="coolwarm", vmin=-1, vmax=1)
fig.colorbar(image, ax=right)
right.set_title('cmap="coolwarm"')

plt.tight_layout()
plt.show()

Which colormap to choose

  • Low to high values: sequentialviridis, cividis, plasma or a single hue such as Blues. Lightness grows evenly, so equal steps in the data look like equal steps in color, also when printed in gray.
  • Above and below a midpoint: divergingcoolwarm, RdBu, PiYG. Set vmin and vmax to the same distance from the midpoint, for example -1 and 1, so zero falls on the neutral color.
  • Categories: qualitativetab10, Set2, Dark2. Distinct colors with no order. Use them for groups, not for numbers.
  • Angles and time of day: cyclictwilight and hsv start and end on the same color.
  • Avoid jet and rainbowBright bands make some changes look larger than they are and hide others, and they are hard to read for color-blind viewers. turbo is a smoother alternative if you need many hues.

Get colors from a colormap

Call a colormap with numbers from 0 to 1 to get RGBA colors, for example to give several lines related colors.

pick_colors.py
Run this chart →
pick_colors.py
import matplotlib
import matplotlib.pyplot as plt
import numpy as np

# Five evenly spaced colors from a colormap, one per line
colors = matplotlib.colormaps["plasma"](np.linspace(0, 0.9, 5))

x = np.linspace(0, 2 * np.pi, 200)
for i, color in enumerate(colors):
    plt.plot(x, np.sin(x + i * 0.5), color=color, linewidth=2, label=f"line {i + 1}")

plt.legend()
plt.title("Colors taken from the plasma colormap")
plt.show()

Change the default colors

Set the color cycle once, and every plot after it uses your colors in order.

color_cycle.py
Run this chart →
color_cycle.py
import matplotlib.pyplot as plt
import numpy as np

# Every new line takes the next color of the cycle
plt.rcParams["axes.prop_cycle"] = plt.cycler(color=["#e63946", "#457b9d", "#2a9d8f", "#f4a261"])

x = np.linspace(0, 10, 100)
for shift in range(4):
    plt.plot(x, np.sin(x) + shift, linewidth=2)

plt.title("A custom default color cycle")
plt.show()

Matplotlib lessons

Frequently asked questions

How do I list all named colors in Matplotlib?

matplotlib.colors.CSS4_COLORS is a dictionary of the 148 CSS color names and their hex codes. BASE_COLORS has the one-letter colors and TABLEAU_COLORS the tab: colors of the default cycle.

How do I reverse a colormap?

Add _r to its name, for example cmap="viridis_r". Every built-in colormap has a reversed version.

How do I get a list of colors from a colormap?

Call the colormap with numbers from 0 to 1: matplotlib.colormaps["viridis"](np.linspace(0, 1, 5)) returns five RGBA colors. resampled(5) returns a colormap with five colors.

How do I change the default colors of lines?

Set the color cycle: plt.rcParams["axes.prop_cycle"] = plt.cycler(color=["#e63946", "#457b9d", "#2a9d8f"]). New plots then use those colors in order.

Which colormap should I use?

Use a perceptually uniform map such as viridis or cividis for values that go from low to high, a diverging map such as coolwarm or RdBu when values go above and below a midpoint, and tab10 for categories. Avoid jet and rainbow, which make some changes look larger than they are.

How do I make a color transparent?

Pass alpha, for example plt.plot(x, y, color="tab:blue", alpha=0.4), or use an RGBA tuple or an 8-digit hex code such as "#1f77b466".