Data Visualization II: Seaborn & Statistical Plots

Data Science Fundamentals

Chapter 8 · Data Visualization II: Seaborn & Statistical Plots

ds1-7's own tip-box promised this chapter's material would "look like a picture of exactly the numbers defined here [ds1-6], not new material." Seaborn earns that promise directly — every function below returns an ordinary Matplotlib Axes object underneath (ds1-7's own foundation, unchanged), with statistical plot types and DataFrame-aware syntax layered on top.

What Seaborn Actually Adds

Three genuine additions over plain Matplotlib: statistical chart types (box, violin, heatmap, pair plots) that ds1-7's own line/bar/scatter/histogram set doesn't include directly; nicer default styling out of the box; and DataFrame-aware syntax — passing a DataFrame plus column names directly, rather than raw arrays pulled out by hand first.

import seaborn as sns

sns.boxplot(data=df, y="revenue")   # DataFrame + column name, not a raw array

Box Plots — ds1-6's Own Vocabulary, Drawn Directly

This is the direct payoff of ds1-6's own promise. Every visual element of a box plot maps onto a term already defined:

Visual elementds1-6 term
The box's bottom edgeQ1 (25th percentile)
The line inside the boxMedian (Q2)
The box's top edgeQ3 (75th percentile)
The box's own heightIQR (Q3 − Q1)
The whiskers1.5 × IQR reach
Individual dots beyond the whiskersFlagged outliers — ds1-4's own IQR rule, drawn

The catering order from ds1-4/ds1-6 would appear here as exactly that: an isolated dot, sitting alone past the upper whisker, visually confirming the same flag the mechanical 1.5×IQR calculation already raised.

Violin Plots — Adding Shape to the Same Summary

sns.violinplot(data=df, y="revenue")

A violin plot shows the same quartile information a box plot does, mirrored into a smoothed, rotated shape — effectively ds1-7's own histogram, reshaped and reflected, layered directly over the box plot's own summary statistics. This matters specifically when a distribution's shape carries real information a box plot alone hides: two distinct clusters of typical order sizes (a genuinely bimodal distribution — small individual orders and separate large group orders, with few orders in between) can produce a perfectly ordinary-looking box plot, since a box plot only ever shows five summary numbers, never the shape connecting them. A violin plot would show that same data as two visible bulges, immediately revealing structure the box plot's own five numbers couldn't.

Heatmaps — Every Pairwise Correlation, One Chart

ds1-7's own scatter plot showed one pair of variables at a time. A correlation heatmap extends that to every numeric column at once:

correlations = df[["quantity", "revenue"]].corr()
sns.heatmap(correlations, annot=True, cmap="coolwarm")

df.corr() computes ds1-6's own Pearson coefficient for every pair of numeric columns simultaneously, producing a square matrix. sns.heatmap() renders that matrix as color intensity — strong positive correlations in one color, strong negative in another, weak correlations pale — with annot=True printing the actual coefficient value inside each cell. With only two numeric columns this collapses to a single interesting cell; the real value appears once a dataset has several numeric columns worth comparing pairwise all at once, exactly the situation ds1-9's own EDA methodology will lean on this for.

Pair Plots — ds1-7's Own Toolkit, Automated

sns.pairplot(df[["quantity", "revenue"]])

sns.pairplot() generates a full grid: a scatter plot for every pair of numeric columns (ds1-7's own scatter plot, run automatically for every combination) plus a histogram for each column against itself along the diagonal (ds1-7's own histogram, likewise automated). It's genuinely everything ds1-7 taught individually, generated for an entire dataset's worth of column pairs in one function call rather than one deliberate chart at a time.

This is where ds1-9 actually starts
A correlation heatmap and a pair plot are, in practice, the first two things run against a genuinely new dataset during real exploratory analysis — a fast, wide first look before deciding which specific relationships deserve a closer, more deliberate chart. ds1-9 formalizes exactly this instinct into a real, repeatable methodology.

Hands-On Exercises

Exercise 1

Using this chapter's own element-by-element table, explain what it means for a box plot to be "ds1-6's own vocabulary, drawn directly," and describe where the ds1-4 catering order would appear on one.

📄 View solution
Exercise 2

Explain, using this chapter's own bimodal-distribution example, why a violin plot can reveal structure a box plot alone hides, even when both are computed from identical data.

📄 View solution
Exercise 3

Explain why this chapter describes sns.pairplot() as "genuinely everything ds1-7 taught individually," specifically identifying which ds1-7 chart type appears on the diagonal and which appears off the diagonal.

📄 View solution

Chapter 8 Quick Reference

  • Seaborn returns ordinary ds1-7 Matplotlib Axes objects underneath — statistical chart types + DataFrame-aware syntax on top
  • Box plot — Q1/median/Q3/IQR/whiskers drawn directly, delivering ds1-6's own promise
  • Violin plot — box-plot summary plus ds1-7's histogram shape, mirrored; reveals bimodal structure a box plot alone hides
  • Heatmapdf.corr() (ds1-6's Pearson coefficient) for every column pair at once, rendered as color
  • Pair plot — ds1-7's scatter plots (off-diagonal) and histograms (diagonal), automated across every column pair
  • Next chapter: Exploratory Data Analysis (EDA) — A Real Methodology