Implementation:DistrictDataLabs Yellowbrick Color Palettes
| Knowledge Sources | |
|---|---|
| Domains | Visualization, Style |
| Last Updated | 2026-02-08 05:00 GMT |
Overview
Concrete tool for managing and applying named color palettes and sequential colormaps, provided by the Yellowbrick style module.
Description
The palettes module defines 20+ named categorical color palettes (e.g., yellowbrick, accent, dark, colorblind) and ColorBrewer sequential palette sequences. The ColorPalette class wraps a list of colors and supports context manager usage for temporarily changing the active palette. Functions color_palette, set_color_codes, and color_sequence provide the primary API for color resolution.
Usage
Import these utilities when customizing visualization colors. Use color_palette to get a list of colors by name, set_color_codes to change matplotlib's shorthand color interpretation, or ColorPalette as a context manager for scoped color changes.
Code Reference
Source Location
- Repository: DistrictDataLabs_Yellowbrick
- File: yellowbrick/style/palettes.py
- Lines: 1-1681
Signature
class ColorPalette(list):
def __init__(self, name_or_list): ...
def __enter__(self): ...
def __exit__(self, *args): ...
def as_hex(self): ...
def as_rgb(self): ...
def plot(self, size=1): ...
def color_palette(palette=None, n_colors=None):
"""Return a ColorPalette object by name or from color list."""
def set_color_codes(palette="accent"):
"""Change how matplotlib interprets color shorthand codes."""
def color_sequence(palette=None, n_colors=None):
"""Return a ListedColormap from a named sequential palette."""
Import
from yellowbrick.style.palettes import color_palette, set_color_codes, ColorPalette, color_sequence
I/O Contract
Inputs (color_palette)
| Name | Type | Required | Description |
|---|---|---|---|
| palette | str or list | No | Palette name or list of colors (default: current palette) |
| n_colors | int | No | Number of colors to return (cycles if needed) |
Outputs
| Name | Type | Description |
|---|---|---|
| palette | ColorPalette | List subclass of color values |
Usage Examples
from yellowbrick.style.palettes import color_palette, ColorPalette
# Get named palette
colors = color_palette("yellowbrick", n_colors=6)
print(colors.as_hex())
# Use as context manager
with ColorPalette("colorblind"):
# All plots inside this block use the colorblind palette
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 4, 9])
plt.show()