Setting Up Seaborn
Install with pip and set a theme. Seaborn's defaults are already more beautiful than Matplotlib's.
Python
# pip install seaborn
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="whitegrid", palette="husl")
# Seaborn ships with example datasets
tips = sns.load_dataset("tips")
print(tips.head())βΆ Output
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3Distribution Plots
Visualise the distribution of a single variable.
Python
tips = sns.load_dataset("tips")
# Histogram + KDE
sns.histplot(data=tips, x="total_bill", hue="sex",
kde=True, bins=20)
plt.title("Bill Distribution by Gender")
plt.show()
# KDE only
sns.kdeplot(data=tips, x="tip", fill=True)
plt.show()Categorical Plots β Boxplot and Violin
Compare distributions across categories.
Python
tips = sns.load_dataset("tips")
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
# Boxplot
sns.boxplot(data=tips, x="day", y="total_bill",
hue="sex", ax=axes[0])
axes[0].set_title("Bill by Day and Gender")
# Violin plot (shows full distribution)
sns.violinplot(data=tips, x="day", y="tip", ax=axes[1])
axes[1].set_title("Tip Distribution by Day")
plt.tight_layout()
plt.show()Ad β 336Γ280
Heatmaps β Correlation Matrices
Visualise correlations between all numerical variables at once.
Python
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
# Correlation matrix
corr = tips.select_dtypes("number").corr()
sns.heatmap(corr, annot=True, fmt=".2f",
cmap="coolwarm", vmin=-1, vmax=1,
square=True, linewidths=0.5)
plt.title("Correlation Heatmap")
plt.show()