🎨Section 5 of 5
🎨 Part 4 — Seaborn
🎬 Why Seaborn?
Seaborn sits on top of Matplotlib and adds two superpowers: beautiful defaults and statistical thinking baked into every chart. One line gives you what would take ten in raw Matplotlib.
Theme & convention
python
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="whitegrid", palette="mako")
plt.figure(figsize=(5, 0.5))
plt.title("Theme set ✓")
plt.axis("off")Distribution — histplot & kdeplot
python
import seaborn as sns, numpy as np, matplotlib.pyplot as plt
sns.set_theme(style="whitegrid")
x = np.random.default_rng(0).normal(75, 12, 500)
plt.figure(figsize=(6, 3.5))
sns.histplot(x, bins=20, kde=True, color="#0f766e")
plt.title("Score distribution with KDE")
plt.tight_layout()Categorical — boxplot
python
import seaborn as sns, pandas as pd, matplotlib.pyplot as plt
sns.set_theme(style="whitegrid")
df = pd.read_csv("sales.csv")
plt.figure(figsize=(6, 3.5))
sns.boxplot(data=df, x="City", y="Revenue", hue="City", palette="mako", legend=False)
plt.title("Revenue spread by city")
plt.tight_layout()Categorical — barplot with hue
python
import seaborn as sns, pandas as pd, matplotlib.pyplot as plt
sns.set_theme(style="whitegrid")
df = pd.read_csv("sales.csv")
plt.figure(figsize=(6, 3.5))
sns.barplot(data=df, x="City", y="Revenue", hue="Product", palette="mako")
plt.title("Revenue by City and Product")
plt.tight_layout()Relationship — scatter + regression
python
import seaborn as sns, pandas as pd, matplotlib.pyplot as plt
sns.set_theme(style="whitegrid")
df = pd.read_csv("sales.csv")
plt.figure(figsize=(6, 3.5))
sns.regplot(data=df, x="Units_Sold", y="Revenue", color="#0f766e",
scatter_kws={"alpha": 0.7})
plt.title("Units vs Revenue (with regression line)")
plt.tight_layout()Heatmap — correlation matrix
python
import seaborn as sns, pandas as pd, matplotlib.pyplot as plt
sns.set_theme()
df = pd.read_csv("sales.csv")
corr = df[["Units_Sold", "Revenue"]].corr()
plt.figure(figsize=(4, 3))
sns.heatmap(corr, annot=True, cmap="mako", fmt=".2f")
plt.title("Correlation")
plt.tight_layout()Pairplot — multi-variable overview
python
import seaborn as sns, pandas as pd, matplotlib.pyplot as plt
sns.set_theme(style="whitegrid")
df = pd.read_csv("sales.csv")
g = sns.pairplot(df, vars=["Units_Sold", "Revenue"], hue="City",
palette="mako", height=2.2)
g.fig.suptitle("Pairwise relationships", y=1.02)🧠
Quick Check
Q1.Which Seaborn function adds a regression line through a scatter?
Q2.What does the `hue` parameter do?
Q3.Best chart to visualize a correlation matrix?
🎉
You finished Lesson 4
You've used NumPy, Pandas, Matplotlib and Seaborn — all running real Python in your browser. Time to build the board report.