Loading...
Loading...
A Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. Great for exploring relationships between variables and visualizing distributions. Use for statistical data visualization, exploratory data analysis (EDA), relationship plots, distribution plots, categorical comparisons, regression visualization, heatmaps, cluster maps, and creating publication-quality statistical graphics from Pandas DataFrames.
npx skill4agent add tondevrel/scientific-agent-skills seabornsns.load_datasetsns.relplotsns.catplotsns.set_themesns.heatmap| Function Type | Examples | Key Characteristic |
|---|---|---|
| Figure-Level | relplot, displot, catplot | Creates its own figure (FacetGrid). Best for subplots (col, row). |
| Axes-Level | scatterplot, histplot, boxplot | Plots onto a specific ax. Best for integration with Matplotlib layouts. |
pip install seabornimport seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# Apply the default theme
sns.set_theme()import seaborn as sns
# Load an example dataset
tips = sns.load_dataset("tips")
# Create a scatter plot with semantic mapping
sns.relplot(
data=tips,
x="total_bill", y="tip",
hue="smoker", style="time", size="size",
)
plt.show()sns.set_theme(style="whitegrid", palette="muted")sns.set_context("paper")sns.plot(x_array, y_array)ax.set_title()import seaborn as sns
import matplotlib.pyplot as plt
# ❌ BAD: Iterating through groups to plot manually
for s in df['species'].unique():
subset = df[df['species'] == s]
plt.scatter(subset['x'], subset['y'], label=s)
# ✅ GOOD: Let Seaborn handle grouping and legend
sns.scatterplot(data=df, x='x', y='y', hue='species')
# ❌ BAD: Mixing Seaborn and Matplotlib titles incorrectly
sns.displot(data=df, x='val')
plt.title("My Title") # ⚠️ Might apply to the wrong axis in a FacetGrid!
# ✅ GOOD: Use the returned object
g = sns.displot(data=df, x='val')
g.set_axis_labels("Value", "Count")
g.figure.suptitle("Correct Global Title", y=1.05)# Multi-faceted scatter plot
sns.relplot(
data=tips, x="total_bill", y="tip",
col="time", hue="day", style="sex",
kind="scatter"
)
# Line plot with automatic aggregation (mean + 95% CI)
fmri = sns.load_dataset("fmri")
sns.relplot(
data=fmri, x="timepoint", y="signal",
hue="event", style="region",
kind="line", errorbar="sd" # "sd" for standard deviation instead of CI
)penguins = sns.load_dataset("penguins")
# Histogram with Kernel Density Estimate
sns.displot(data=penguins, x="flipper_length_mm", hue="species", kde=True)
# Bivariate distribution (Heatmap style)
sns.displot(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species", kind="kde")
# Empirical Cumulative Distribution (ECDF)
sns.displot(data=penguins, x="flipper_length_mm", hue="species", kind="ecdf")# Boxplot (Show quartiles and outliers)
sns.catplot(data=tips, x="day", y="total_bill", kind="box")
# Violin plot (Show density and quartiles)
sns.catplot(data=tips, x="day", y="total_bill", hue="sex", kind="violin", split=True)
# Swarm plot (Show every point without overlap)
sns.catplot(data=tips, x="day", y="total_bill", kind="swarm")
# Bar plot (Show mean and error bars)
sns.catplot(data=tips, x="day", y="total_bill", kind="bar", errorbar=("pi", 95))# Simple regression with scatter
sns.regplot(data=tips, x="total_bill", y="tip")
# Faceted regression
sns.lmplot(data=tips, x="total_bill", y="tip", col="smoker", hue="time")
# Logistic regression (for binary data)
sns.lmplot(data=df, x="variable", y="binary_outcome", logistic=True)flights = sns.load_dataset("flights").pivot(index="month", columns="year", values="passengers")
# Heatmap
plt.figure(figsize=(10, 8))
sns.heatmap(flights, annot=True, fmt="d", cmap="YlGnBu")
# Cluster map (Hierarchical clustering)
sns.clustermap(flights, standard_scale=1, cmap="mako")# JointPlot (Scatter + Marginals)
sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species", kind="kde")
# PairPlot (All-against-all relations)
sns.pairplot(data=penguins, hue="species", corner=True)
# Custom FacetGrid
g = sns.FacetGrid(tips, col="time", row="sex")
g.map(sns.scatterplot, "total_bill", "tip")# Set overall look
sns.set_style("darkgrid") # white, dark, whitegrid, ticks
sns.set_context("talk") # paper, notebook, talk, poster
# Custom palettes
sns.set_palette("husl") # Set global palette
my_pal = sns.color_palette("rocket", as_cmap=True) # Get palette as object
# Viewing a palette
sns.palplot(sns.color_palette("Set2"))def initial_eda(df, target_col):
"""Generate basic visual summary of a dataset."""
# 1. Distribution of target
sns.displot(data=df, x=target_col, kde=True)
# 2. Pairwise relations of numeric features
sns.pairplot(data=df, hue=target_col if df[target_col].nunique() < 10 else None)
# 3. Correlation heatmap
plt.figure(figsize=(12, 10))
sns.heatmap(df.corr(numeric_only=True), annot=True, cmap='coolwarm', fmt=".2f")
# initial_eda(iris, "species")def plot_experiment_results(df):
"""Plot results of an experiment with multiple conditions."""
g = sns.catplot(
data=df, kind="bar",
x="condition", y="metric", hue="group",
palette="viridis", alpha=.6, height=6
)
g.despine(left=True)
g.set_axis_labels("Experimental Condition", "Accuracy (%)")
g.legend.set_title("User Group")
return gdef plot_trends(df, time_col, val_col, cat_col):
"""Visualizes trends over time with confidence intervals."""
plt.figure(figsize=(12, 6))
sns.lineplot(
data=df, x=time_col, y=val_col, hue=cat_col,
marker="o", err_style="bars"
)
plt.xticks(rotation=45)
plt.tight_layout()# ❌ Problem: Legend covers data in narrow plots
# ✅ Solution: Move legend manually using Matplotlib logic
g = sns.scatterplot(data=tips, x="total_bill", y="tip", hue="day")
sns.move_legend(g, "upper left", bbox_to_anchor=(1, 1))# ❌ Problem: sns.pairplot(large_df) hangs
# ✅ Solution: Sample data or use simpler plots
sns.pairplot(df.sample(1000), hue='category')
# OR use hist instead of scatter
sns.jointplot(data=df, x='x', y='y', kind="hist")# ❌ Problem: Categorical labels on X-axis overlap
# ✅ Solution: Rotate labels using Matplotlib
g = sns.boxplot(data=df, x='very_long_category_name', y='value')
g.set_xticklabels(g.get_xticklabels(), rotation=45, horizontalalignment='right')relplotdisplotcatplotdata=sns.set_theme()huesizestylesns.set_context("paper")ax.set_title()df.sample()