Seaborn Cheatsheet¶
Installation¶
| Platform | Command |
|---|---|
| Ubuntu/Debian | pip install seaborn or sudo pip3 install seaborn |
| macOS | pip3 install seaborn or conda install seaborn |
| Windows | pip install seaborn or conda install seaborn |
| Conda (all platforms) | conda install -c conda-forge seaborn |
| Specific version | pip install seaborn==0.12.2 |
| With dependencies | pip install seaborn[all] |
| Upgrade existing | pip install --upgrade seaborn |
| Verify installation | python -c "import seaborn as sns; print(sns.__version__)" |
Basic Commands¶
| Command | Description |
|---|---|
import seaborn as sns |
Standard import convention |
sns.set_theme() |
Apply default Seaborn styling |
sns.set_style("whitegrid") |
Set plot style (whitegrid, darkgrid, white, dark, ticks) |
sns.set_context("notebook") |
Set scaling context (paper, notebook, talk, poster) |
sns.histplot(data=df, x='col', kde=True) |
Create histogram with kernel density estimate |
sns.kdeplot(data=df, x='col') |
Plot kernel density estimate |
sns.boxplot(data=df, x='cat', y='val') |
Create box plot for categorical data |
sns.violinplot(data=df, x='cat', y='val') |
Create violin plot (box + KDE) |
sns.scatterplot(data=df, x='x', y='y', hue='cat') |
Create scatter plot with color grouping |
sns.lineplot(data=df, x='time', y='val') |
Create line plot with confidence intervals |
sns.barplot(data=df, x='cat', y='val') |
Create bar plot with error bars |
sns.countplot(data=df, x='cat') |
Count occurrences of categorical values |
sns.heatmap(df.corr(), annot=True) |
Create annotated correlation heatmap |
sns.pairplot(df, hue='species') |
Create scatter plot matrix for all variables |
sns.regplot(data=df, x='x', y='y') |
Scatter plot with linear regression line |
sns.stripplot(data=df, x='cat', y='val') |
Categorical scatter plot (all points visible) |
sns.swarmplot(data=df, x='cat', y='val') |
Non-overlapping categorical scatter plot |
sns.jointplot(data=df, x='x', y='y') |
Scatter plot with marginal distributions |
sns.color_palette() |
Get current color palette |
sns.set_palette("husl") |
Set color palette (husl, Set1, Set2, deep, etc.) |
Advanced Usage¶
| Command | Description |
|---|---|
sns.lmplot(data=df, x='x', y='y', hue='cat', col='group') |
Regression plot with faceting by columns |
sns.catplot(data=df, x='cat', y='val', kind='violin', col='group') |
Categorical plot with faceting |
sns.relplot(data=df, x='x', y='y', kind='scatter', col='cat', row='group') |
Relational plot with row/column faceting |
sns.clustermap(df.corr(), method='ward', metric='euclidean') |
Hierarchical clustered heatmap |
sns.kdeplot(data=df, x='x', y='y', fill=True, levels=10) |
2D bivariate KDE plot |
sns.ecdfplot(data=df, x='val', hue='cat', stat='proportion') |
Empirical cumulative distribution function |
sns.histplot(data=df, x='val', hue='cat', multiple='stack') |
Stacked histogram for multiple categories |
sns.violinplot(data=df, x='cat', y='val', hue='group', split=True) |
Split violin plot for comparison |
sns.regplot(data=df, x='x', y='y', order=3) |
Polynomial regression (order 3) |
sns.regplot(data=df, x='x', y='y', robust=True) |
Robust regression (outlier-resistant) |
sns.residplot(data=df, x='x', y='y') |
Plot residuals of regression |
sns.pointplot(data=df, x='cat', y='val', hue='group') |
Point plot with error bars |
sns.diverging_palette(250, 10, as_cmap=True) |
Create diverging color palette |
sns.cubehelix_palette(start=.5, rot=-.75) |
Create cubehelix color palette |
sns.despine(left=True, bottom=True) |
Remove plot spines for cleaner look |
sns.rugplot(data=df, x='val') |
Add rug plot (marginal tick marks) |
g = sns.FacetGrid(df, col='cat', row='group'); g.map(plt.scatter, 'x', 'y') |
Custom faceted plot grid |
sns.barplot(data=df, x='val', y='cat', orient='h', ci=95) |
Horizontal bar plot with 95% CI |
sns.heatmap(df, cmap='RdYlGn', center=0, vmin=-1, vmax=1) |
Heatmap with centered diverging colors |
sns.move_legend(ax, "upper left", bbox_to_anchor=(1, 1)) |
Reposition legend outside plot |
Configuration¶
Theme and Style Configuration¶
# Comprehensive theme setup
sns.set_theme(
style="darkgrid", # whitegrid, darkgrid, white, dark, ticks
palette="deep", # Color palette
font="sans-serif", # Font family
font_scale=1.2, # Scale all fonts
color_codes=True, # Use color codes
rc={ # Matplotlib rcParams
"figure.figsize": (12, 8),
"axes.labelsize": 14,
"axes.titlesize": 16,
"xtick.labelsize": 12,
"ytick.labelsize": 12,
"legend.fontsize": 12
}
)
Context-Based Scaling¶
# Scale plots for different contexts
sns.set_context(
"talk", # paper, notebook, talk, poster
font_scale=1.5,
rc={"lines.linewidth": 2.5}
)
Custom Color Palettes¶
# Define custom color palette
custom_colors = ["#FF6B6B", "#4ECDC4", "#45B7D1", "#FFA07A", "#98D8C8"]
sns.set_palette(sns.color_palette(custom_colors))
# Or use built-in palettes
sns.set_palette("Set2", n_colors=8)
Temporary Style Context¶
# Apply style temporarily
with sns.axes_style("white"):
sns.boxplot(data=df, x='category', y='value')
plt.show()
# Original style restored after block
Figure-Level Function Parameters¶
# Common parameters for figure-level functions
g = sns.relplot(
data=df,
x='x', y='y',
hue='category',
col='group',
col_wrap=3, # Wrap columns after 3
height=4, # Height of each facet
aspect=1.5, # Aspect ratio (width = height * aspect)
palette='viridis',
legend='full' # 'auto', 'brief', 'full', False
)
Common Use Cases¶
Use Case 1: Exploratory Data Analysis¶
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
# Load data
df = pd.read_csv('data.csv')
# Quick overview with pairplot
sns.pairplot(df, hue='target_variable', diag_kind='kde')
plt.savefig('pairplot.png', dpi=300, bbox_inches='tight')
# Correlation heatmap
plt.figure(figsize=(10, 8))
sns.heatmap(df.corr(), annot=True, fmt='.2f', cmap='coolwarm', center=0)
plt.title('Feature Correlation Matrix')
plt.tight_layout()
plt.savefig('correlation.png', dpi=300)
Use Case 2: Distribution Comparison¶
# Compare distributions across categories
fig, axes = plt.subplots(2, 2, figsize=(14, 10))
# Histogram with KDE
sns.histplot(data=df, x='value', hue='category', kde=True, ax=axes[0, 0])
axes[0, 0].set_title('Distribution with KDE')
# Violin plot
sns.violinplot(data=df, x='category', y='value', ax=axes[0, 1])
axes[0, 1].set_title('Violin Plot')
# Box plot with swarm overlay
sns.boxplot(data=df, x='category', y='value', ax=axes[1, 0])
sns.swarmplot(data=df, x='category', y='value', color='black', alpha=0.3, ax=axes[1, 0])
axes[1, 0].set_title('Box + Swarm Plot')
# ECDF
sns.ecdfplot(data=df, x='value', hue='category', ax=axes[1, 1])
axes[1, 1].set_title('Cumulative Distribution')
plt.tight_layout()
plt.show()
Use Case 3: Time Series Visualization¶
# Time series with confidence intervals
df['date'] = pd.to_datetime(df['date'])
# Line plot with shaded confidence interval
plt.figure(figsize=(14, 6))
sns.lineplot(
data=df,
x='date',
y='value',
hue='category',
style='category',
markers=True,
dashes=False,
ci=95,
err_style='band'
)
plt.xticks(rotation=45)
plt.title('Time Series with 95% Confidence Intervals')
plt.tight_layout()
plt.show()
Use Case 4: Multi-Variable Faceted Analysis¶
# Create faceted plot for complex relationships
g = sns.FacetGrid(
df,
col='region',
row='product_type',
hue='customer_segment',
height=4,
aspect=1.2,
margin_titles=True
)
g.map_dataframe(sns.scatterplot, x='price', y='sales', alpha=0.6)
g.add_legend(title='Customer Segment')
g.set_axis_labels('Price ($)', 'Sales Volume')
g.set_titles(col_template="{col_name}", row_template="{row_name}")
g.tight_layout()
plt.savefig('faceted_analysis.png', dpi=300, bbox_inches='tight')
Use Case 5: Statistical Regression Analysis¶
# Multiple regression visualizations
fig, axes = plt.subplots(1, 3, figsize=(18, 5))
# Linear regression
sns.regplot(data=df, x='feature1', y='target', ax=axes[0])
axes[0].set_title('Linear Regression')
# Polynomial regression
sns.regplot(data=df, x='feature2', y='target', order=3, ax=axes[1])
axes[1].set_title('Polynomial Regression (Order 3)')
# Residual plot
sns.residplot(data=df, x='feature1', y='target', ax=axes[2])
axes[2].set_title('Residual Plot')
axes[2].axhline(0, ls='--', color='red', alpha=0.5)
plt.tight_layout()
plt.show()
Best Practices¶
-
Use figure-level functions for faceting: Functions like
relplot(),catplot(), andlmplot()automatically handle figure and axes creation, making faceted plots easier to manage -
Choose appropriate plot types: Use violin plots for distribution comparison, box plots for outlier detection, swarm plots for small datasets, and strip plots for medium-sized datasets
-
Leverage the
dataparameter: Always use thedataparameter with pandas DataFrames for cleaner code and better integration with data manipulation workflows -
Set themes early: Call
sns.set_theme()orsns.set_style()at the beginning of your script to ensure consistent styling across all plots -
Use semantic mappings: Take advantage of
hue,size, andstyleparameters to encode additional dimensions in your visualizations without creating separate plots -
Save high-resolution figures: Use
plt.savefig('file.png', dpi=300, bbox_inches='tight')for publication-quality images with proper margins -
Combine Seaborn with Matplotlib: Use Seaborn for quick statistical plots and Matplotlib for fine-tuned customization (titles, labels, annotations, etc.)
-
Handle large datasets efficiently: For datasets with >10,000 points, consider using
rasterized=Truein scatter plots or aggregating data before plotting -
Use color palettes strategically: Choose sequential palettes for ordered data, diverging palettes for data with a meaningful center, and qualitative palettes for categorical data
-
Document your color choices: When creating visualizations for colorblind audiences, use colorblind-friendly palettes like
"colorblind"or test withsns.color_palette("colorblind")
Troubleshooting¶
| Issue | Solution |
|---|---|
| Plot not displaying | Add plt.show() at the end or use %matplotlib inline in Jupyter notebooks |
| Legend overlapping plot | Use sns.move_legend(ax, "upper left", bbox_to_anchor=(1, 1)) or plt.legend(bbox_to_anchor=(1.05, 1)) |
| Categorical axis labels overlapping | Rotate labels with plt.xticks(rotation=45, ha='right') or use horizontal plots with orient='h' |
| Heatmap annotations too small | Increase figure size plt.figure(figsize=(12, 10)) or adjust font size annot_kws={"size": 10} |
| Color palette not applying | Set palette before creating plot with sns.set_palette() or use palette parameter directly in plot function |
| FacetGrid plots too small | Increase height parameter (e.g., height=5) and adjust aspect ratio (e.g., aspect=1.5) |
| Missing confidence intervals | Ensure sufficient data points; CI requires multiple observations per x-value. Use ci=None to disable |
| Seaborn overriding Matplotlib settings | Reset with sns.reset_orig() or sns.reset_defaults() to restore Matplotlib defaults |
| Memory issues with large datasets | Use rasterized=True for scatter plots or downsample data before plotting |
| KDE plot looks wrong | Adjust bw_adjust parameter (e.g., bw_adjust=0.5 for narrower bandwidth) or use cut=0 to limit range |
| Regression line not fitting data | Try polynomial regression with order=2 or order=3, or use robust=True for outlier resistance |
| Heatmap colors not centered | Use center=0 parameter and diverging colormap like cmap='RdBu_r' for data with meaningful zero point |
| Plot style not persisting | Use sns.set_theme() instead of sns.set() (deprecated) or set rc parameters explicitly |
| Facet titles cut off | Add plt.tight_layout() or use g.tight_layout() for FacetGrid objects |
| Import errors after installation | Restart Python kernel/IDE or verify installation with pip show seaborn and check dependencies |