Statistical data visualization library for Python built on matplotlib
—
Generate and customize color palettes including sequential, diverging, qualitative, and perceptually uniform palettes. These functions provide sophisticated color schemes for data visualization with options for specific color spaces and custom palette creation.
Generate evenly spaced colors in HUSL (human-friendly HSL) color space.
def husl_palette(n_colors=6, h=0.01, s=0.9, l=0.65, as_cmap=False):
"""
Get a set of evenly spaced colors in HUSL hue space.
Parameters:
- n_colors: int, number of colors in the palette
- h: float or tuple, hue value or range (0-1)
- s: float or tuple, saturation value or range (0-1)
- l: float or tuple, lightness value or range (0-1)
- as_cmap: bool, return matplotlib Colormap object
Returns:
list of RGB tuples or matplotlib Colormap
"""Generate evenly spaced colors in HLS color space.
def hls_palette(n_colors=6, h=0.01, l=0.6, s=0.65, as_cmap=False):
"""
Get a set of evenly spaced colors in HLS hue space.
Parameters:
- n_colors: int, number of colors in the palette
- h: float or tuple, hue value or range (0-1)
- l: float or tuple, lightness value or range (0-1)
- s: float or tuple, saturation value or range (0-1)
- as_cmap: bool, return matplotlib Colormap object
Returns:
list of RGB tuples or matplotlib Colormap
"""Create sequential palettes using the cubehelix color system.
def cubehelix_palette(
n_colors=6,
start=0,
rot=0.4,
gamma=1.0,
hue=0.8,
light=0.85,
dark=0.15,
reverse=False,
as_cmap=False
):
"""
Make a sequential palette from the cubehelix system.
Parameters:
- n_colors: int, number of colors in the palette
- start: float, starting color (0-3)
- rot: float, rotation amount (-1 to 1)
- gamma: float, gamma correction factor
- hue: float, saturation intensity
- light: float, lightest color (0-1)
- dark: float, darkest color (0-1)
- reverse: bool, reverse the palette direction
- as_cmap: bool, return matplotlib Colormap object
Returns:
list of RGB tuples or matplotlib Colormap
"""Create palettes that blend from light colors to a specified color.
def light_palette(color, n_colors=6, reverse=False, as_cmap=False, input="rgb"):
"""
Make a sequential palette that blends from light to a color.
Parameters:
- color: str or tuple, base color specification
- n_colors: int, number of colors in the palette
- reverse: bool, reverse the palette direction
- as_cmap: bool, return matplotlib Colormap object
- input: str, color input format ("rgb", "hls", "husl", "xkcd")
Returns:
list of RGB tuples or matplotlib Colormap
"""Create palettes that blend from dark colors to a specified color.
def dark_palette(color, n_colors=6, reverse=False, as_cmap=False, input="rgb"):
"""
Make a sequential palette that blends from dark to a color.
Parameters:
- color: str or tuple, base color specification
- n_colors: int, number of colors in the palette
- reverse: bool, reverse the palette direction
- as_cmap: bool, return matplotlib Colormap object
- input: str, color input format ("rgb", "hls", "husl", "xkcd")
Returns:
list of RGB tuples or matplotlib Colormap
"""Create diverging palettes between two colors in HUSL space.
def diverging_palette(
h_neg,
h_pos,
s=75,
l=50,
sep=1,
n=6,
center="light",
as_cmap=False
):
"""
Make a diverging palette between two HUSL colors.
Parameters:
- h_neg: float, hue for negative side (0-359)
- h_pos: float, hue for positive side (0-359)
- s: float, saturation (0-100)
- l: float, lightness (0-100)
- sep: int, separation between negative and positive sides
- n: int, number of colors in the palette
- center: str, center color ("light", "dark")
- as_cmap: bool, return matplotlib Colormap object
Returns:
list of RGB tuples or matplotlib Colormap
"""Blend between multiple colors to create custom palettes.
def blend_palette(colors, n_colors=6, as_cmap=False, input="rgb"):
"""
Blend between a list of colors to make a new palette.
Parameters:
- colors: list, sequence of colors to blend
- n_colors: int, number of colors in resulting palette
- as_cmap: bool, return matplotlib Colormap object
- input: str, color input format ("rgb", "hls", "husl", "xkcd")
Returns:
list of RGB tuples or matplotlib Colormap
"""Create palettes using XKCD color names.
def xkcd_palette(colors):
"""
Make a palette from XKCD color names.
Parameters:
- colors: list, XKCD color names
Returns:
list of RGB tuples
Available colors accessible via sns.colors.xkcd_rgb dictionary
"""Create palettes using Crayola crayon color names.
def crayon_palette(colors):
"""
Make a palette from Crayola crayon colors.
Parameters:
- colors: list, crayon color names
Returns:
list of RGB tuples
Available colors accessible via sns.colors.crayons dictionary
"""Return discrete colors from matplotlib colormaps.
def mpl_palette(name, n_colors=6, as_cmap=False):
"""
Return discrete colors from a matplotlib palette.
Parameters:
- name: str, matplotlib colormap name
- n_colors: int, number of discrete colors
- as_cmap: bool, return matplotlib Colormap object
Returns:
list of RGB tuples or matplotlib Colormap
"""import seaborn as sns
import matplotlib.pyplot as plt
# Show a palette
palette = sns.color_palette("husl", 8)
sns.palplot(palette)
plt.show()
# Use in a plot
tips = sns.load_dataset("tips")
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="day", palette="husl")
plt.show()# Custom HUSL palette
husl_colors = sns.husl_palette(6, h=0.5, s=0.8, l=0.6)
sns.palplot(husl_colors)
plt.show()# Cubehelix sequential palette
cube_palette = sns.cubehelix_palette(8, start=0.5, rot=-0.75)
sns.palplot(cube_palette)
plt.show()
# Use as colormap
data = sns.load_dataset("flights").pivot("month", "year", "passengers")
sns.heatmap(data, cmap=sns.cubehelix_palette(as_cmap=True))
plt.show()# Light to color palette
light_blue = sns.light_palette("navy", 6)
sns.palplot(light_blue)
plt.show()
# Dark to color palette
dark_green = sns.dark_palette("seagreen", 6, reverse=True)
sns.palplot(dark_green)
plt.show()# Custom diverging palette
diverging = sns.diverging_palette(220, 20, n=7, center="dark")
sns.palplot(diverging)
plt.show()
# Use in heatmap
correlation = tips.select_dtypes(include=['float64']).corr()
sns.heatmap(correlation, cmap=diverging, center=0, annot=True)
plt.show()# Blend between multiple colors
colors = ["red", "orange", "yellow", "green", "blue"]
blended = sns.blend_palette(colors, 10)
sns.palplot(blended)
plt.show()# Use XKCD color names
xkcd_colors = ["windows blue", "amber", "greyish", "faded green", "dusty purple"]
xkcd_palette = sns.xkcd_palette(xkcd_colors)
sns.palplot(xkcd_palette)
plt.show()# Use Crayola crayon colors
crayon_colors = ["Red", "Blue", "Yellow", "Green", "Purple"]
crayon_palette = sns.crayon_palette(crayon_colors)
sns.palplot(crayon_palette)
plt.show()# Extract colors from matplotlib colormap
viridis_discrete = sns.mpl_palette("viridis", 8)
sns.palplot(viridis_discrete)
plt.show()# Different palettes for different contexts
with sns.color_palette("husl", 8):
# This plot uses HUSL colors
sns.boxplot(data=tips, x="day", y="total_bill", hue="smoker")
plt.show()
# Back to default palette
sns.boxplot(data=tips, x="day", y="total_bill", hue="smoker")
plt.show()Access to named color collections:
# XKCD colors (954 colors)
xkcd_colors = sns.colors.xkcd_rgb
print(list(xkcd_colors.keys())[:10]) # First 10 color names
# Crayola colors (163 colors)
crayon_colors = sns.colors.crayons
print(list(crayon_colors.keys())[:10]) # First 10 color names# Color specifications
ColorSpec = str | tuple[float, float, float] | tuple[float, float, float, float]
PaletteSpec = str | list[ColorSpec] | dict
# Input formats
ColorInput = Literal["rgb", "hls", "husl", "xkcd"]
# Palette types
QualitativePalette = Literal["deep", "muted", "bright", "pastel", "dark", "colorblind"]
SequentialPalette = Literal["rocket", "mako", "flare", "crest"]
DivergingPalette = Literal["vlag", "icefire"]Install with Tessl CLI
npx tessl i tessl/pypi-seaborn