Introduction
Seaborn is a statistical data visualization library built on Matplotlib. It provides a high-level interface for creating informative and attractive statistical graphics.
Why Seaborn?
- Statistical plots: Built-in support for statistical visualizations
- Beautiful defaults: Attractive color palettes and styles
- DataFrame integration: Works directly with Pandas DataFrames
Installation and Setup
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
sns.set_theme(style="darkgrid")
tips = pd.read_csv('tips.csv')
Distribution Plots
# Distribution plot
sns.histplot(tips['total_bill'], kde=True)
# Multiple distributions
sns.displot(data=tips, x='total_bill', hue='sex', kind='kde')
# Box plot
sns.boxplot(x='day', y='total_bill', data=tips)
Relational Plots
# Scatter plot with regression line
sns.regplot(x='total_bill', y='tip', data=tips)
# Scatter with hue
sns.scatterplot(x='total_bill', y='tip', hue='day', data=tips)
# Line plot with confidence intervals
sns.lineplot(x='size', y='tip', data=tips)
Categorical Plots
# Bar plot
sns.barplot(x='day', y='total_bill', data=tips)
# Count plot
sns.countplot(x='day', data=tips)
# Violin plot
sns.violinplot(x='day', y='total_bill', data=tips)
# Strip plot
sns.stripplot(x='day', y='total_bill', data=tips)
Multi-Plot Grids
g = sns.FacetGrid(tips, col='time', row='smoker')
g.map_dataframe(sns.scatterplot, x='total_bill', y='tip')
g.add_legend()
g = sns.PairGrid(tips.select_dtypes(include=[np.number]))
g.map_diag(sns.histplot)
g.map_offdiag(sns.scatterplot)
Heatmaps
# Correlation heatmap
corr = tips.corr()
sns.heatmap(corr, annot=True, cmap='coolwarm', center=0)
# Pivot table heatmap
pivot = tips.pivot_table(values='tip', index='day', columns='size')
sns.heatmap(pivot, annot=True)
Customization
sns.set_style("whitegrid")
sns.set_palette("husl")
sns.set_context("notebook", font_scale=1.2)
plt.figure(figsize=(10, 6))
sns.boxplot(x='day', y='total_bill', data=tips, palette='Set2')
Key Takeaways
- Seaborn simplifies statistical visualization
- Use FacetGrid for complex multi-panel plots
- Customizable themes and palettes for professional plots