← Về thư mục
📄 / / profiles / r-and-d / raw_evidence / scientific-agent-skills / skills / seaborn / references / palettes_and_theming.md

Color Palettes, Theming, and Aesthetics

Qualitative, sequential, and diverging palettes, colorblind-safe choices, and theme, context, and style control.

Color Palettes

Seaborn provides carefully designed color palettes for different data types:

Qualitative Palettes (Categorical Data)

Distinguish categories through hue variation: - "deep" - Default, vivid colors - "muted" - Softer, less saturated - "pastel" - Light, desaturated - "bright" - Highly saturated - "dark" - Dark values - "colorblind" - Safe for color vision deficiency

sns.set_palette("colorblind")
sns.color_palette("Set2")

Sequential Palettes (Ordered Data)

Show progression from low to high values: - "rocket", "mako" - Wide luminance range (good for heatmaps) - "flare", "crest" - Restricted luminance (good for points/lines) - "viridis", "magma", "plasma" - Matplotlib perceptually uniform

sns.heatmap(data, cmap='rocket')
sns.kdeplot(data=df, x='x', y='y', cmap='mako', fill=True)

Diverging Palettes (Centered Data)

Emphasize deviations from a midpoint: - "vlag" - Blue to red - "icefire" - Blue to orange - "coolwarm" - Cool to warm - "Spectral" - Rainbow diverging

sns.heatmap(correlation_matrix, cmap='vlag', center=0)

Custom Palettes

# Create custom palette
custom = sns.color_palette("husl", 8)

# Light to dark gradient
palette = sns.light_palette("seagreen", as_cmap=True)

# Diverging palette from hues
palette = sns.diverging_palette(250, 10, as_cmap=True)

Theming and Aesthetics

Set Theme

set_theme() controls overall appearance:

# Set complete theme
sns.set_theme(style='whitegrid', palette='pastel', font='sans-serif')

# Reset to defaults
sns.set_theme()

Styles

Control background and grid appearance: - "darkgrid" - Gray background with white grid (default) - "whitegrid" - White background with gray grid - "dark" - Gray background, no grid - "white" - White background, no grid - "ticks" - White background with axis ticks

sns.set_style("whitegrid")

# Remove spines
sns.despine(left=False, bottom=False, offset=10, trim=True)

# Temporary style
with sns.axes_style("white"):
    sns.scatterplot(data=df, x='x', y='y')

Contexts

Scale elements for different use cases: - "paper" - Smallest (default) - "notebook" - Slightly larger - "talk" - Presentation slides - "poster" - Large format

sns.set_context("talk", font_scale=1.2)

# Temporary context
with sns.plotting_context("poster"):
    sns.barplot(data=df, x='category', y='value')