Statistical data visualization library for Python built on matplotlib
—
Configure plot aesthetics, themes, color palettes, and plotting contexts with extensive customization options for publication-quality graphics. These functions provide comprehensive control over the visual appearance of seaborn and matplotlib plots.
Set overall aesthetic parameters and make seaborn the default matplotlib style.
def set_theme(
context="notebook",
style="darkgrid",
palette="deep",
font="sans-serif",
font_scale=1,
color_codes=True,
rc=None
):
"""
Set multiple theme parameters in one step.
Parameters:
- context: str, plotting context ("paper", "notebook", "talk", "poster")
- style: str, axes style ("darkgrid", "whitegrid", "dark", "white", "ticks")
- palette: str or list, color palette
- font: str, font family
- font_scale: float, scaling factor for font sizes
- color_codes: bool, use seaborn color codes
- rc: dict, matplotlib rcParams to override
"""
def set(
context="notebook",
style="darkgrid",
palette="deep",
font="sans-serif",
font_scale=1,
color_codes=True,
rc=None
):
"""
Alias for set_theme() - sets aesthetic parameters in one step.
"""Control the appearance of plot axes and backgrounds.
def axes_style(style=None, rc=None):
"""
Get the parameters that control the general style of the plots.
Parameters:
- style: str, style name ("darkgrid", "whitegrid", "dark", "white", "ticks")
- rc: dict, parameter overrides
Returns:
dict of matplotlib rcParams
Can be used as context manager:
with sns.axes_style("white"):
# plots with white background
"""
def set_style(style, rc=None):
"""
Set the aesthetic style of the plots.
Parameters:
- style: str, style name or dict of parameters
- rc: dict, parameter overrides
"""Control the scale of plot elements for different contexts.
def plotting_context(context=None, font_scale=1, rc=None):
"""
Get the parameters that control the scaling of plot elements.
Parameters:
- context: str, context name ("paper", "notebook", "talk", "poster")
- font_scale: float, scaling factor for font sizes
- rc: dict, parameter overrides
Returns:
dict of matplotlib rcParams
Can be used as context manager:
with sns.plotting_context("poster"):
# plots scaled for poster presentation
"""
def set_context(context, font_scale=1, rc=None):
"""
Set the plotting context parameters.
Parameters:
- context: str, context name or dict of parameters
- font_scale: float, scaling factor for font sizes
- rc: dict, parameter overrides
"""Set and retrieve color palettes for consistent color schemes.
def set_palette(palette, n_colors=None, desat=None, color_codes=False):
"""
Set the matplotlib color cycle using a seaborn palette.
Parameters:
- palette: str, list, or dict, palette specification
- n_colors: int, number of colors in cycle
- desat: float, desaturation factor (0-1)
- color_codes: bool, use seaborn color codes
"""
def color_palette(palette=None, n_colors=None, desat=None, as_cmap=False):
"""
Return a list of colors or continuous colormap defining a palette.
Parameters:
- palette: str, list, or None, palette specification
- n_colors: int, number of colors to return
- desat: float, desaturation factor (0-1)
- as_cmap: bool, return matplotlib Colormap object
Returns:
list of RGB tuples or matplotlib Colormap
"""Restore default settings.
def reset_defaults():
"""Restore all seaborn style settings to default values."""
def reset_orig():
"""Restore all matplotlib rcParams to original settings before seaborn import."""Control interpretation of matplotlib color abbreviations.
def set_color_codes(palette="deep"):
"""
Change how matplotlib color shorthands are interpreted.
Parameters:
- palette: str, palette name for color codes
Calling this changes the meaning of abbreviations like 'b', 'g', 'r', etc.
"""import seaborn as sns
import matplotlib.pyplot as plt
# Set overall theme
sns.set_theme(style="whitegrid", palette="pastel")
# Create a plot - will use theme settings
tips = sns.load_dataset("tips")
sns.boxplot(data=tips, x="day", y="total_bill")
plt.show()# Temporarily change style
with sns.axes_style("white"):
sns.scatterplot(data=tips, x="total_bill", y="tip")
plt.show()
# Back to default style
sns.boxplot(data=tips, x="day", y="total_bill")
plt.show()# Scale for poster presentation
sns.set_context("poster", font_scale=1.2)
sns.barplot(data=tips, x="day", y="total_bill")
plt.show()
# Reset to notebook context
sns.set_context("notebook")# Set custom color palette
custom_colors = ["#FF6B6B", "#4ECDC4", "#45B7D1", "#FFA07A"]
sns.set_palette(custom_colors)
# Plots will use custom colors
sns.countplot(data=tips, x="day")
plt.show()# Enable seaborn color codes
sns.set_color_codes("muted")
# Now 'b', 'g', 'r' use seaborn colors
plt.plot([1, 2, 3], [1, 4, 2], 'b-') # Uses seaborn blue
plt.show()# Temporarily scale for presentation
with sns.plotting_context("talk", font_scale=1.5):
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
sns.scatterplot(data=tips, x="total_bill", y="tip", ax=axes[0,0])
sns.boxplot(data=tips, x="day", y="total_bill", ax=axes[0,1])
sns.histplot(data=tips, x="total_bill", ax=axes[1,0])
sns.barplot(data=tips, x="day", y="total_bill", ax=axes[1,1])
plt.tight_layout()
plt.show()# Reset all seaborn settings
sns.reset_defaults()
# Or reset to pre-seaborn matplotlib settings
sns.reset_orig()# Detailed theme customization
sns.set_theme(
context="notebook",
style="ticks",
palette="Set2",
font="serif",
font_scale=1.1,
rc={
"axes.spines.right": False,
"axes.spines.top": False,
"axes.linewidth": 1.2,
"grid.alpha": 0.4
}
)
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="smoker")
plt.show()# Style options
AxesStyle = Literal["darkgrid", "whitegrid", "dark", "white", "ticks"]
PlottingContext = Literal["paper", "notebook", "talk", "poster"]
# Palette specifications
PaletteSpec = str | list | dict | None
# RC parameters
RCParams = dict[str, Any] # matplotlib rcParams dictionaryInstall with Tessl CLI
npx tessl i tessl/pypi-seaborn