Skip to main content
Albero Academy

Colors

Specify colors many ways, and use them to communicate clearly.

Customizing Plots8 min

Colour is Matplotlib's most powerful — and most misused — channel. You can specify it many ways, and using it deliberately is what separates a clear chart from a confusing one.

Specifying colours

every way to name a colour
ax.plot(x, y, color="crimson") # named CSS colour
ax.plot(x, y, color="#2563EB") # hex
ax.plot(x, y, color="C0") # the Nth colour in the cycle
ax.plot(x, y, color=(0.1, 0.2, 0.5)) # RGB tuple
ax.plot(x, y, color="tab:blue", alpha=0.7) # tableau palette + transparency

The colour cycle

When you don't specify a colour, Matplotlib walks a default cycle (C0, C1, C2…), so successive lines get distinct colours automatically. That's why two ax.plot calls look different without any effort.

Key takeaways

  1. 01Colours can be named, hex, RGB tuples, "CN" cycle refs, or "tab:" palette names.
  2. 02Without a colour, Matplotlib cycles C0, C1, C2… so lines differ automatically.
  3. 03Use colour to encode meaning, not for decoration.
  4. 04Prefer colour-blind-safe palettes like tab10 and viridis.

Finished this chapter?

Mark it complete to track your progress through Matplotlib.