matplotlib
named colors
data visualization
Python
plotting

Named colors in matplotlib

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Matplotlib supports many named colors that make plotting code easier to read than raw numeric tuples. Named colors are useful for quick prototypes and reusable style systems. Understanding available color namespaces helps you choose readable and consistent palettes.

Core Sections

Basic Named Color Usage

You can pass named colors directly to plotting calls.

python
1import matplotlib.pyplot as plt
2
3x = [1, 2, 3, 4]
4y = [2, 3, 5, 4]
5
6plt.plot(x, y, color="steelblue", linewidth=2)
7plt.scatter(x, y, color="darkorange")
8plt.title("Named Colors Example")
9plt.show()

Names are more expressive than manual RGB values in many cases.

Explore Available Named Colors

Matplotlib exposes color dictionaries through matplotlib.colors.

python
1from matplotlib import colors as mcolors
2
3print(len(mcolors.CSS4_COLORS))
4print(list(mcolors.CSS4_COLORS.keys())[:10])

This includes CSS4 names, Tableau colors, and XKCD colors.

Compare Color Name Sets

Different name sets can be useful for different contexts.

python
1from matplotlib import colors as mcolors
2
3print("Base:", list(mcolors.BASE_COLORS.keys()))
4print("Tableau:", list(mcolors.TABLEAU_COLORS.keys())[:5])
5print("XKCD sample:", list(mcolors.XKCD_COLORS.keys())[:5])

Tableau colors are often a good default for categorical plots.

Validate Color Names Programmatically

If user input controls colors, validate names before plotting.

python
1from matplotlib import colors as mcolors
2
3
4def is_valid_named_color(name: str) -> bool:
5    return mcolors.is_color_like(name)
6
7print(is_valid_named_color("navy"))
8print(is_valid_named_color("not-a-color"))

Validation prevents runtime errors in dynamic chart systems.

Build Consistent Theme Mappings

For production dashboards, map semantic roles to fixed colors.

python
1THEME = {
2    "success": "seagreen",
3    "warning": "goldenrod",
4    "error": "crimson",
5    "neutral": "slategray",
6}

Semantic mappings improve consistency across charts and teams.

Accessibility and Contrast Considerations

Named colors are convenient, but accessibility still matters. Verify contrast in legends, labels, and line thickness. For categorical series, choose palettes with distinguishable hues and test grayscale readability when possible.

You can combine named colors with linestyle and marker differences to improve interpretability for color-blind users.

Export and Reproducibility

When saving figures, explicit colors make rendered output stable across environments.

python
plt.savefig("chart.png", dpi=150, bbox_inches="tight")

Document theme color decisions so report updates remain visually consistent.

Build Palette Utilities for Teams

For shared analytics projects, wrap color choices in helper functions instead of hardcoding names in every notebook. This prevents accidental style drift and keeps charts consistent.

python
1TEAM_COLORS = {
2    "primary": "royalblue",
3    "secondary": "darkorange",
4    "success": "seagreen",
5    "danger": "crimson",
6}
7
8
9def color_for(role: str) -> str:
10    return TEAM_COLORS.get(role, "slategray")

Then use semantic roles in plotting code. Teams can update palette definitions once and propagate style changes everywhere.

Validate Visual Accessibility in Practice

Beyond choosing named colors, run accessibility checks on exported figures. Test with grayscale previews, contrast checks, and color-blind simulation tools. Add marker variations and line styles for categories so interpretation does not rely on hue alone.

For important business dashboards, keep a short review checklist covering label contrast, legend clarity, and distinguishability of adjacent series. This turns color accessibility into a repeatable quality process.

Codified palette rules also improve consistency during report handoffs between analysts and engineering teams.

A shared style guide with approved named colors and examples can significantly reduce review friction and keep visual communication aligned across recurring reports.

Consistent color semantics improve chart comprehension speed.

Reusable style helpers reduce long-term maintenance cost in plotting codebases.

Common Pitfalls

  • Using arbitrary color names without a theme and creating inconsistent visuals.
  • Choosing low-contrast named colors that reduce readability.
  • Assuming all users distinguish closely related hues equally.
  • Accepting unchecked user color input and causing plotting failures.
  • Mixing many color systems without documenting design intent.

Summary

  • Named colors make Matplotlib code readable and maintainable.
  • Explore color dictionaries to choose from supported namespaces.
  • Validate dynamic color input with built-in helpers.
  • Use semantic color mappings for consistent dashboards.
  • Consider accessibility and contrast in every chart design.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.