Seaborn: Statistical Visualization
Get publication-quality statistical charts in one line — scatterplots with hue, boxplots, violin plots, correlation heatmaps, and pairplots with seaborn.
Matplotlib can draw anything, but common statistical charts take a lot of boilerplate. Seaborn sits on top of matplotlib and specializes in exactly those charts: it understands DataFrames directly, maps columns to colors and styles for you, and looks great out of the box.
Where these cells run
Seaborn isn't available in this page's browser runtime, so the sns. code
blocks below are for the downloadable notebook or Google Colab (seaborn is
preinstalled there). Two PyRunner cells near the end recreate the same looks
with plain matplotlib so you can still practice in the browser.
What seaborn adds
Compare drawing a scatter plot colored by category. In matplotlib you filter
the DataFrame per group and call scatter in a loop; in seaborn you just
name the columns:
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips") # a classic demo dataset: restaurant bills
tips.head()# one line: x, y, and "color by smoker status"
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="smoker")
plt.show()That data= + column-names interface is seaborn's core idea. The three
semantic mappings you'll use constantly:
hue— map a column to colorstyle— map a column to marker shapesize— map a column to marker size
sns.scatterplot(data=tips, x="total_bill", y="tip",
hue="time", style="smoker", size="size")
plt.show()One call, five variables on screen. Use this power sparingly — two semantics per chart is usually the readability limit.
Distributions: histplot
sns.histplot is matplotlib's histogram plus statistical extras — most
usefully, an optional smoothed density curve (KDE) and per-group overlays
with hue:
sns.histplot(data=tips, x="total_bill", bins=25, kde=True)
plt.show()# overlay distributions per group
sns.histplot(data=tips, x="total_bill", hue="time", bins=25)
plt.show()Categories: boxplot, violinplot, countplot
For a numeric variable split by category, seaborn's category plots are the biggest time-savers.
A boxplot summarizes each group with its median, quartile box, whiskers, and outlier dots:
sns.boxplot(data=tips, x="day", y="total_bill")
plt.show()A violin plot replaces the box with the full density shape — better when groups might be bimodal (two bumps), which a box would hide:
sns.violinplot(data=tips, x="day", y="total_bill", hue="sex", split=True)
plt.show()A countplot is a bar chart of frequencies — value_counts() as a picture:
sns.countplot(data=tips, x="day", hue="smoker")
plt.show()Correlation heatmaps
A heatmap paints a matrix as colored cells. Its killer use case: the correlation matrix of your numeric columns, which shows every pairwise relationship at once:
penguins = sns.load_dataset("penguins")
corr = penguins.select_dtypes("number").corr()
sns.heatmap(corr, annot=True, fmt=".2f", cmap="coolwarm", vmin=-1, vmax=1)
plt.show()annot=True prints the numbers in each cell; vmin/vmax=(-1, 1) anchors the
color scale so that white really means "no correlation".
Pairplot: the one-line dataset overview
sns.pairplot draws a grid of scatter plots for every pair of numeric columns
(with distributions on the diagonal). It's the classic first move in
exploratory data analysis:
sns.pairplot(penguins, hue="species", vars=["bill_length_mm", "flipper_length_mm", "body_mass_g"])
plt.show()On the penguins dataset this instantly reveals that the three species form separable clusters — exactly the kind of insight you want before any modeling.
Themes
One call restyles every subsequent chart — including plain matplotlib ones, since seaborn just configures matplotlib underneath:
sns.set_theme(style="whitegrid", palette="deep") # do this once, at the top
sns.boxplot(data=tips, x="day", y="total_bill")
plt.show()Other styles worth trying: "darkgrid" (the default), "white", "ticks".
In-browser practice (plain matplotlib)
You can approximate seaborn's two signature moves with matplotlib. First,
a hue-style scatter — loop over groups, one color per group:
And a boxplot comparing groups — matplotlib's boxplot takes a list of
arrays, one per category:
Same insight as sns.boxplot — weekend bills run higher and spread wider —
just with a little more code. That's the trade in a nutshell.
Check your understanding
Q1.What is seaborn's relationship to matplotlib?
Q2.In sns.scatterplot(data=df, x="bill", y="tip", hue="day"), what does hue do?
Q3.When would a violin plot beat a boxplot?
Q4.What does sns.heatmap(df.corr(), annot=True) display?
Q5.Why do the seaborn examples in this lesson use plain code fences instead of runnable cells?
Exercise: Explore the penguins dataset in Colab
In the downloadable notebook or Colab, load sns.load_dataset("penguins") and
build five charts: (1) a scatterplot of flipper length vs body mass with
hue="species" and style="sex", (2) a boxplot of bill depth per species,
(3) a countplot of species per island, (4) a correlation heatmap of the numeric
columns with annotations, and (5) a pairplot of three numeric columns colored
by species. Set a whitegrid theme first. Which two variables are most strongly
correlated?
That wraps up the data toolkit — you can now load, clean, query, and visualize data, which is everything you need to start the machine learning course.