Seaborn is a Python visualization library based on Matplotlib that provides a high-level interface for drawing attractive and informative statistical graphics. It’s designed to work well with Pandas DataFrames and makes complex plots simple.
Why Seaborn?
- Beautiful Defaults: Professional-looking plots out of the box
- Statistical Focus: Built-in support for statistical visualizations
- DataFrame Integration: Works naturally with Pandas
- Less Code: Complex plots with simple function calls
- Themes: Consistent, attractive styling
Key Plot Types
Distribution Plots
import seaborn as sns
import pandas as pd
# Histogram with KDE
sns.histplot(data=df, x='reaction_time', kde=True)
# Box and violin plots
sns.boxplot(data=df, x='condition', y='reaction_time')
sns.violinplot(data=df, x='condition', y='reaction_time')
Relationship Plots
# Scatter with regression line
sns.regplot(data=df, x='age', y='performance')
# Scatter matrix
sns.pairplot(data=df, hue='group')
# Correlation heatmap
corr = df.corr()
sns.heatmap(corr, annot=True, cmap='coolwarm')
Categorical Plots
# Bar plot with error bars
sns.barplot(data=df, x='condition', y='accuracy', errorbar='se')
# Point plot for trends
sns.pointplot(data=df, x='session', y='performance', hue='group')
# Count plot
sns.countplot(data=df, x='response_type')
Multi-Panel Figures
# FacetGrid for subplots by category
g = sns.FacetGrid(df, col='subject', row='condition')
g.map(sns.histplot, 'reaction_time')
Common Use Cases in Research
Behavioral Analysis
- Compare reaction times across conditions
- Visualize learning curves over sessions
- Show accuracy distributions by group
- Display trial-by-trial performance
Neural Data
- Compare firing rates across neurons/conditions
- Visualize spike count distributions
- Show calcium signal amplitudes
- Plot correlation matrices for neural populations
Experimental Results
- Show group differences with confidence intervals
- Display longitudinal measurements
- Visualize multi-factor experiments
- Create publication-ready summary figures
Getting Started
Install Seaborn:
conda install seaborn
# or
pip install seaborn
Basic example:
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
# Set theme
sns.set_theme(style='whitegrid')
# Sample data
df = pd.DataFrame({
'condition': ['A', 'B'] * 50,
'reaction_time': np.random.normal([0.5, 0.6], 0.1, 100)
})
# Create plot
sns.violinplot(data=df, x='condition', y='reaction_time')
plt.ylabel('Reaction Time (s)')
plt.title('Reaction Times by Condition')
plt.show()
Seaborn Themes
Apply consistent styling:
# Built-in themes
sns.set_theme(style='darkgrid') # Grid background
sns.set_theme(style='whitegrid') # White with grid
sns.set_theme(style='dark') # Dark background
sns.set_theme(style='white') # Clean white
sns.set_theme(style='ticks') # With tick marks
# Customize
sns.set_theme(
context='paper', # or 'notebook', 'talk', 'poster'
palette='deep', # Color palette
font_scale=1.2 # Scale all fonts
)
Tips
- Use
sns.set_theme()at the start of your notebook - Combine with Matplotlib for fine-tuning
- Use
hue,size,styleparameters to show additional variables - Try
sns.pairplot()for quick exploratory analysis - Use
errorbar='se'orerrorbar='sd'for error bars - Save color palettes for consistency:
pal = sns.color_palette('Set2') - Use
sns.despine()to remove chart borders