Data Visualization with Python¶
Chapter 3: Turning Numbers into Insight¶
Why This Chapter Matters¶
Chapter 2 taught the reader how to summarize data numerically. This chapter teaches the complementary discipline: how to make structure visible. A statistic can be correct and still incomplete. A mean can hide subgroups. A correlation can hide curvature. A model score can hide calibration failure. A table can contain the truth while still failing to communicate it.
Visualization is not decoration added after analysis. It is part of analysis. A good figure helps the analyst discover patterns, helps the reader evaluate claims, and helps a team notice what a single number would have concealed. This matters in AI/ML because models often fail for reasons that are visible before they are mathematical: skewed features, outliers, class imbalance, nonlinearity, subgroup separation, drift, and leakage.
The chapter teaches three layers of Python visualization. Matplotlib is the foundation: figures, axes, artists, layout, annotation, color, and file export. Seaborn builds a statistical visualization grammar on top of Matplotlib: distributions, categorical comparisons, pair plots, and heatmaps from Pandas data. Plotly adds interactivity: hover text, zooming, trace toggles, 3D views, and browser-native HTML graphics.
The goal is not to collect chart types. The goal is to choose the right visual question. Are we showing one variable, two variables, many variables, categories, time, uncertainty, or interaction? Does the plot help the reader see the structure honestly? Does it preserve units, labels, accessibility, and scale? Those questions matter more than style.
For technical reference, keep the official Matplotlib usage guide, Matplotlib gallery, Seaborn tutorial, Plotly Python documentation, and Plotly Express API reference nearby.
Section 3.0: Setup, Imports, and Reproducibility¶
Visualization work depends on both data and rendering state. The setup cell imports the numerical stack, the tabular stack, static plotting libraries, and interactive plotting libraries used in this chapter. It also sets a reproducible random generator so the synthetic datasets and plots stay stable across runs.
Matplotlib supplies the core figure machinery. Seaborn supplies statistical plotting functions that work naturally with Pandas DataFrames. Plotly Express provides concise interactive plots, while Plotly Graph Objects gives lower-level control when a figure needs custom traces or layout.
The setup also establishes a simple plotting style. These defaults remove distracting spines, lighten gridlines, and make later figures consistent. Style is not cosmetic trivia; a consistent visual language lets the reader spend attention on the data rather than on changing graphic conventions.
Run this setup cell before the later examples.
# === Chapter 3: Setup ===
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib.patches as mpatches
import seaborn as sns
from scipy import stats
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import warnings
warnings.filterwarnings("ignore", category=FutureWarning)
# Reproducibility
rng = np.random.default_rng(42)
# matplotlib global style — matches Ch02/Ch04/Ch05
plt.rcParams.update({
"figure.figsize": (8, 4),
"axes.grid": True,
"grid.alpha": 0.3,
"axes.spines.top": False,
"axes.spines.right": False,
})
sns.set_theme(style="whitegrid")
# Print versions
import scipy
print(f"NumPy {np.__version__} | Pandas {pd.__version__} | SciPy {scipy.__version__}")
print(f"Matplotlib {plt.matplotlib.__version__} | Seaborn {sns.__version__}")
import plotly
print(f"Plotly {plotly.__version__}")
print("\nChapter 3 environment ready.")
Section 3.1: The Matplotlib Architecture¶
Matplotlib can be used in two ways. The quick scripting interface is pyplot, usually imported as plt. It is convenient for exploration because commands such as plt.plot() and plt.hist() create figures quickly. The more durable interface is the object-oriented API, where a Figure contains one or more Axes, and each Axes owns its lines, bars, labels, ticks, annotations, legends, and images.
The object-oriented style is the one to learn for serious work. It makes explicit which panel is being modified, which matters as soon as a figure has more than one plot. The pattern fig, ax = plt.subplots() creates a figure and one axes. The pattern fig, axes = plt.subplots(rows, columns) creates a grid of axes. Each ax then receives commands such as ax.plot, ax.scatter, ax.set_title, and ax.legend.
This model also explains why Matplotlib can feel verbose. It is not merely drawing a line; it is building an object tree. That tree gives the author precise control over layout, labels, scales, colors, legends, export formats, and publication details. The first code cell contrasts the quick pyplot style with the object-oriented style used throughout the rest of the book.
# === Section 3.1: pyplot vs OO API ===
# --- pyplot style (quick exploration) ---
x = np.linspace(0, 2 * np.pi, 200)
plt.figure(figsize=(10, 3))
plt.subplot(1, 2, 1)
plt.plot(x, np.sin(x), color="steelblue")
plt.title("pyplot style: plt.plot()")
plt.ylabel("sin(x)")
# --- OO style (recommended) ---
fig, ax = plt.subplots(1, 1, figsize=(5, 3))
ax.plot(x, np.sin(x), color="steelblue", linewidth=2, label="sin(x)")
ax.plot(x, np.cos(x), color="#ef8a62", linewidth=2, linestyle="--", label="cos(x)")
ax.set_title("OO style: ax.plot()")
ax.set_xlabel("Radians")
ax.set_ylabel("Amplitude")
ax.legend(frameon=True, fancybox=True, shadow=True)
plt.tight_layout()
plt.show()
print("→ The OO style (fig, ax) gives explicit control over each panel.")
print(" We use this style exclusively throughout the book.")
Section 3.2: Single-Panel Plots¶
Most everyday data visualization begins with four single-panel plot types.
A line plot shows change along an ordered axis, especially time or a continuous input. A bar chart compares values across categories. A scatter plot shows the relationship between two quantitative variables. A histogram shows the shape of one quantitative distribution.
These plots are simple, but they are not interchangeable. A bar chart is appropriate when the x-axis contains categories. A histogram is appropriate when one continuous variable is grouped into bins. A scatter plot is appropriate when each point represents a paired observation. A line plot implies order and continuity; using it for unordered categories suggests a connection that may not exist.
The next example uses a synthetic clinical table to place all four plot types side by side. The point is not the synthetic data itself, but the decision rule: match the visual form to the structure of the question.
# === Section 3.2: Four Fundamental Plot Types ===
# Synthetic clinical dataset
n = 150
df_clinic = pd.DataFrame({
"Patient_ID": range(1, n + 1),
"Age": rng.normal(55, 12, n).astype(int).clip(18, 90),
"Systolic_BP": rng.normal(130, 18, n).round(1),
"LDL_Cholesterol": rng.normal(120, 35, n).round(1),
"Treatment": rng.choice(["Drug A", "Drug B", "Placebo"], n),
"Months_Enrolled": np.arange(1, n + 1) % 12 + 1,
})
df_clinic["BMI"] = rng.normal(27, 4.5, n).round(1).clip(16, 50)
fig, axes = plt.subplots(2, 2, figsize=(14, 10))
# (a) Line plot — mean systolic BP by enrollment month
monthly_bp = df_clinic.groupby("Months_Enrolled")["Systolic_BP"].mean()
axes[0, 0].plot(monthly_bp.index, monthly_bp.values, "o-", color="steelblue", linewidth=2)
axes[0, 0].set_title("(a) Line Plot — Mean Systolic BP by Month")
axes[0, 0].set_xlabel("Month Enrolled")
axes[0, 0].set_ylabel("Mean Systolic BP (mmHg)")
# (b) Bar chart — patient count by treatment group
counts = df_clinic["Treatment"].value_counts().sort_index()
colors = ["#2166ac", "#67a9cf", "#ef8a62"]
axes[0, 1].bar(counts.index, counts.values, color=colors, edgecolor="white", width=0.6)
axes[0, 1].set_title("(b) Bar Chart — Patients per Treatment Group")
axes[0, 1].set_ylabel("Count")
for i, (label, val) in enumerate(zip(counts.index, counts.values)):
axes[0, 1].text(i, val + 1, str(val), ha="center", fontweight="bold")
# (c) Scatter plot — Age vs LDL
axes[1, 0].scatter(df_clinic["Age"], df_clinic["LDL_Cholesterol"],
alpha=0.6, c="steelblue", edgecolors="white", s=40)
axes[1, 0].set_title("(c) Scatter Plot — Age vs LDL Cholesterol")
axes[1, 0].set_xlabel("Age (years)")
axes[1, 0].set_ylabel("LDL Cholesterol (mg/dL)")
# (d) Histogram — BMI distribution
axes[1, 1].hist(df_clinic["BMI"], bins=20, color="#67a9cf", edgecolor="white", alpha=0.85)
axes[1, 1].axvline(df_clinic["BMI"].mean(), color="red", linestyle="--", linewidth=2, label=f'Mean = {df_clinic["BMI"].mean():.1f}')
axes[1, 1].set_title("(d) Histogram — BMI Distribution")
axes[1, 1].set_xlabel("BMI (kg/m²)")
axes[1, 1].set_ylabel("Frequency")
axes[1, 1].legend()
plt.suptitle("Four Fundamental Plot Types", fontsize=14, fontweight="bold", y=1.01)
plt.tight_layout()
plt.show()
print("→ These four plot types cover ~80% of daily visualization needs.")
print(" Master them before reaching for more exotic charts.")
Section 3.3: Multi-Panel Figures¶
Real analysis often asks more than one visual question at a time. A single plot may show a relationship, while neighboring panels show marginal distributions, subgroup comparisons, or diagnostic views. Multi-panel figures make that comparison possible without forcing the reader to flip between disconnected charts.
Matplotlib offers several layout tools. plt.subplots is the simplest and works well for equal-sized grids. GridSpec is more flexible: one panel can span multiple rows or columns while smaller panels sit beside it. subplot_mosaic lets authors name panels in a layout map.
Good multi-panel design is disciplined. Panels should share a clear purpose, aligned scales when comparison requires it, and enough labels that each panel can be read without guessing. A dense figure is useful only when the density is organized.
The GridSpec example below uses a large scatter plot with smaller marginal histograms, a common pattern when the main relationship and the distributions of its variables both matter.
# === Section 3.3: GridSpec — Unequal Panel Sizes ===
fig = plt.figure(figsize=(14, 6))
gs = gridspec.GridSpec(2, 3, figure=fig, hspace=0.4, wspace=0.3)
# Large panel spanning left two columns, both rows
ax_main = fig.add_subplot(gs[:, :2])
ax_main.scatter(df_clinic["Age"], df_clinic["Systolic_BP"],
c=df_clinic["LDL_Cholesterol"], cmap="RdYlBu_r",
alpha=0.7, edgecolors="white", s=50)
ax_main.set_title("Age vs Systolic BP (color = LDL)", fontsize=12)
ax_main.set_xlabel("Age")
ax_main.set_ylabel("Systolic BP")
# Top-right: histogram of age
ax_top = fig.add_subplot(gs[0, 2])
ax_top.hist(df_clinic["Age"], bins=15, color="#67a9cf", edgecolor="white")
ax_top.set_title("Age Distribution")
ax_top.set_xlabel("Age")
# Bottom-right: histogram of systolic BP
ax_bot = fig.add_subplot(gs[1, 2])
ax_bot.hist(df_clinic["Systolic_BP"], bins=15, color="#ef8a62", edgecolor="white")
ax_bot.set_title("BP Distribution")
ax_bot.set_xlabel("Systolic BP")
plt.suptitle("GridSpec Layout — Main Plot + Marginal Histograms",
fontsize=13, fontweight="bold", y=1.02)
plt.tight_layout()
plt.show()
print("→ GridSpec is ideal when panels need different sizes.")
print(" The main scatter occupies 2/3 of the figure; marginals fill the rest.")
Section 3.4: Annotations and Styling¶
A plot should not require the reader to inspect the code in order to understand it. Titles, axis labels, units, legends, annotations, and arrows are part of the argument the figure is making.
The strongest titles state the visual question or takeaway, not merely the variable names. Axis labels include units. Legends identify encodings without covering the data. Annotations point to peaks, thresholds, reference ranges, outliers, or clinically important regions. Mathematical labels can use Matplotlib's math text syntax when notation is clearer than prose.
Styling also includes restraint. Removing top and right spines can reduce clutter. Light gridlines can guide the eye without dominating the data. A legend shadow may be helpful in one figure and distracting in another. The standard is not decoration; the standard is whether the figure becomes easier to read accurately.
The following example uses a normal density curve to demonstrate labels, filled regions, a legend, an annotation arrow, and mathematical axis text.
# === Section 3.4: Annotations & Styling Demo ===
x = np.linspace(-3, 3, 300)
y = stats.norm.pdf(x)
fig, ax = plt.subplots(figsize=(10, 5))
ax.plot(x, y, color="#2166ac", linewidth=2.5)
ax.fill_between(x, y, where=(x >= -1) & (x <= 1), alpha=0.3, color="#67a9cf",
label="68% (±1σ)")
ax.fill_between(x, y, where=((x >= -2) & (x < -1)) | ((x > 1) & (x <= 2)),
alpha=0.2, color="#d1e5f0", label="95% (±2σ)")
# Annotate the peak
ax.annotate("Peak at μ = 0",
xy=(0, stats.norm.pdf(0)),
xytext=(1.5, 0.35),
fontsize=11,
arrowprops=dict(arrowstyle="->", color="red", lw=1.5),
color="red", fontweight="bold")
# LaTeX axis labels
ax.set_xlabel(r"$z = \frac{x - \mu}{\sigma}$", fontsize=13)
ax.set_ylabel(r"$f(z) = \frac{1}{\sqrt{2\pi}} e^{-z^2/2}$", fontsize=13)
ax.set_title("Annotated Normal Distribution with LaTeX Labels", fontsize=13)
# Custom legend
ax.legend(loc="upper left", frameon=True, fancybox=True, shadow=True, fontsize=10)
# Move bottom spine to y=0 for a textbook look
ax.spines["bottom"].set_position(("data", 0))
ax.spines["left"].set_position(("data", -3.2))
plt.tight_layout()
plt.show()
print("→ Annotations transform a chart from 'data dump' to 'visual argument.'")
print(" Always label axes with units, annotate key features, and use legends.")
Section 3.5: Color Palettes and Colormaps¶
Color is a data channel. It can encode magnitude, group membership, positive versus negative deviation, uncertainty, or emphasis. It can also mislead if used carelessly.
Sequential colormaps are appropriate when values move from low to high. Diverging colormaps are appropriate when values depart from a meaningful center, such as zero correlation or no change from baseline. Qualitative palettes are appropriate for unordered categories, where colors should distinguish groups without implying rank.
Accessibility matters. Many readers have color-vision differences. Many figures are printed in grayscale, copied into slides, or viewed on poor screens. A good palette preserves contrast, does not rely on red-green distinctions alone, and uses labels or markers when color alone would be ambiguous.
Matplotlib's viridis is a useful default because it is perceptually ordered and colorblind-aware. Seaborn palettes are convenient for categorical plots. The example below compares sequential, diverging, and qualitative choices so the differences are visible.
# === Section 3.5: Color Palettes Showcase ===
fig, axes = plt.subplots(2, 3, figsize=(16, 8))
# (a) Sequential — viridis
data = np.random.randn(10, 10).cumsum(axis=1)
im0 = axes[0, 0].imshow(data, cmap="viridis", aspect="auto")
axes[0, 0].set_title('Sequential: "viridis"')
fig.colorbar(im0, ax=axes[0, 0], shrink=0.8)
# (b) Sequential — Blues
im1 = axes[0, 1].imshow(data, cmap="Blues", aspect="auto")
axes[0, 1].set_title('Sequential: "Blues"')
fig.colorbar(im1, ax=axes[0, 1], shrink=0.8)
# (c) Diverging — RdBu_r
centered = np.random.randn(10, 10)
im2 = axes[0, 2].imshow(centered, cmap="RdBu_r", aspect="auto", vmin=-3, vmax=3)
axes[0, 2].set_title('Diverging: "RdBu_r"')
fig.colorbar(im2, ax=axes[0, 2], shrink=0.8)
# (d) Qualitative — seaborn palettes
palette_names = ["Set2", "tab10", "Paired"]
for i, pname in enumerate(palette_names):
pal = sns.color_palette(pname, 8)
for j, c in enumerate(pal):
axes[1, i].barh(j, 1, color=c, edgecolor="white")
axes[1, i].set_title(f'Qualitative: "{pname}"')
axes[1, i].set_xlim(0, 1.2)
axes[1, i].set_yticks(range(8))
axes[1, i].set_yticklabels([f"Color {k}" for k in range(8)])
axes[1, i].invert_yaxis()
plt.suptitle("Colormap Types: Sequential, Diverging, and Qualitative",
fontsize=14, fontweight="bold", y=1.01)
plt.tight_layout()
plt.show()
print("→ Choose colormaps deliberately:")
print(" Sequential for ordered data, Diverging for centered data,")
print(" Qualitative for unordered categories.")
print('→ "viridis" is colorblind-safe and prints well in grayscale.')
Section 3.6: Saving Figures¶
A figure is not finished until it can leave the notebook. Reports, manuscripts, regulatory documents, dashboards, and presentations all impose different export requirements.
Raster formats such as PNG store pixels. They are convenient for slides, web pages, and screenshots, but resolution matters. Vector formats such as PDF and SVG store geometric instructions. They scale cleanly and are usually better for publication graphics, diagrams, and documents that may be resized.
Matplotlib's savefig is the core export function. The usual professional habits are to set a meaningful file name, choose the right format, use adequate DPI for raster output, and pass bbox_inches="tight" when excess whitespace should be removed. Transparent backgrounds can be useful for slides or compositing, but they should be chosen intentionally.
The code cell saves the same figure as PNG, PDF, and SVG so the reader can see that export is part of the visualization workflow, not an afterthought.
# === Section 3.6: Saving Figures (Demonstration) ===
fig, ax = plt.subplots(figsize=(8, 4))
x = np.linspace(0, 4 * np.pi, 300)
ax.plot(x, np.sin(x) * np.exp(-x / 10), color="steelblue", linewidth=2)
ax.set_title("Damped Sine Wave")
ax.set_xlabel("Time")
ax.set_ylabel("Amplitude")
# Demonstrate savefig syntax (saving to a temporary location)
import tempfile, os
tmp_dir = tempfile.mkdtemp()
for fmt in ["png", "pdf", "svg"]:
path = os.path.join(tmp_dir, f"demo_figure.{fmt}")
fig.savefig(path, dpi=300, bbox_inches="tight")
plt.tight_layout()
plt.show()
# Report saved files
for fmt in ["png", "pdf", "svg"]:
path = os.path.join(tmp_dir, f"demo_figure.{fmt}")
size_kb = os.path.getsize(path) / 1024
print(f" {fmt.upper():>4s}: {size_kb:6.1f} KB — {path}")
print(f"\n→ PNG at 300 DPI is good for slides; PDF/SVG for publications.")
print(" Always use bbox_inches='tight' to eliminate excess whitespace.")
# Clean up temp files
import shutil
shutil.rmtree(tmp_dir)
Section 3.7: Seaborn for Statistical Visualization¶
Seaborn is built on top of Matplotlib, but it changes the level of expression. Instead of drawing every graphical element manually, the author usually passes a DataFrame and names the columns that should map to axes, categories, colors, or facets.
This is why Seaborn is so useful for exploratory data analysis. Distribution plots show shape. Categorical plots compare groups. Relational plots show associations. Matrix plots show correlations or other rectangular summaries. Many functions return Matplotlib Axes, so Seaborn and Matplotlib can be combined in the same figure.
The important habit is to let the statistical question choose the plot family. Use histograms, KDEs, ECDFs, and rugs for continuous distributions. Use box plots, violin plots, swarm plots, strip plots, and bar plots for category-by-value comparisons. Use scatter and line plots for relationships. Use heatmaps for matrix-shaped summaries.
The next examples show distribution and categorical families. Notice that these are not merely prettier defaults; they are compact ways to ask statistical questions.
# === Section 3.7a: Distribution Plots ===
normal_data = rng.normal(100, 15, 500)
skewed_data = rng.exponential(20, 500) + 60
fig, axes = plt.subplots(2, 3, figsize=(16, 9))
# (a) histplot with KDE
sns.histplot(normal_data, bins=25, kde=True, color="steelblue", ax=axes[0, 0])
axes[0, 0].set_title("histplot + KDE overlay")
# (b) kdeplot — multiple distributions
sns.kdeplot(normal_data, color="#2166ac", linewidth=2, label="Normal", ax=axes[0, 1])
sns.kdeplot(skewed_data, color="#ef8a62", linewidth=2, label="Right-skewed", ax=axes[0, 1])
axes[0, 1].legend()
axes[0, 1].set_title("kdeplot — Kernel Density Estimate")
# (c) ecdfplot
sns.ecdfplot(normal_data, color="steelblue", linewidth=2, ax=axes[0, 2])
sns.ecdfplot(skewed_data, color="#ef8a62", linewidth=2, ax=axes[0, 2])
axes[0, 2].set_title("ecdfplot — Empirical CDF")
# (d) histplot bivariate (2-D density)
x = rng.normal(0, 1, 1000)
y = 0.6 * x + rng.normal(0, 0.8, 1000)
sns.histplot(x=x, y=y, bins=30, cmap="Blues", ax=axes[1, 0])
axes[1, 0].set_title("2-D histplot (bivariate)")
# (e) kdeplot bivariate (contour)
sns.kdeplot(x=x, y=y, cmap="Blues", fill=True, levels=6, ax=axes[1, 1])
axes[1, 1].set_title("2-D kdeplot (filled contour)")
# (f) rugplot + kdeplot
sns.kdeplot(normal_data[:100], color="steelblue", linewidth=2, ax=axes[1, 2])
sns.rugplot(normal_data[:100], color="steelblue", alpha=0.5, ax=axes[1, 2])
axes[1, 2].set_title("kdeplot + rugplot")
plt.suptitle("Seaborn Distribution Plots", fontsize=14, fontweight="bold", y=1.01)
plt.tight_layout()
plt.show()
print("→ seaborn's distribution functions are the fastest way to visualize")
print(" shape, spread, and density of continuous variables.")
# === Section 3.7b: Categorical Plots ===
# Build a clinical comparison dataset
n_cat = 200
df_cat = pd.DataFrame({
"Treatment": np.repeat(["Drug A", "Drug B", "Placebo"], [70, 70, 60]),
"Response": np.concatenate([
rng.normal(78, 10, 70), # Drug A — moderate
rng.normal(85, 8, 70), # Drug B — best
rng.normal(72, 12, 60), # Placebo — lowest
]),
"Site": rng.choice(["Site 1", "Site 2", "Site 3"], 200),
})
fig, axes = plt.subplots(2, 3, figsize=(16, 10))
# (a) boxplot
sns.boxplot(data=df_cat, x="Treatment", y="Response",
palette=["#2166ac", "#67a9cf", "#ef8a62"], ax=axes[0, 0])
axes[0, 0].set_title("boxplot")
# (b) violinplot
sns.violinplot(data=df_cat, x="Treatment", y="Response",
palette=["#2166ac", "#67a9cf", "#ef8a62"],
inner="quartile", ax=axes[0, 1])
axes[0, 1].set_title("violinplot (inner=quartile)")
# (c) swarmplot
sns.swarmplot(data=df_cat, x="Treatment", y="Response",
palette=["#2166ac", "#67a9cf", "#ef8a62"],
size=4, ax=axes[0, 2])
axes[0, 2].set_title("swarmplot (every point)")
# (d) boxplot + stripplot (layered)
sns.boxplot(data=df_cat, x="Treatment", y="Response",
palette=["#2166ac", "#67a9cf", "#ef8a62"],
boxprops=dict(alpha=0.4), ax=axes[1, 0])
sns.stripplot(data=df_cat, x="Treatment", y="Response",
color="black", alpha=0.4, size=3, jitter=True, ax=axes[1, 0])
axes[1, 0].set_title("boxplot + stripplot overlay")
# (e) barplot (mean + CI)
sns.barplot(data=df_cat, x="Treatment", y="Response",
palette=["#2166ac", "#67a9cf", "#ef8a62"],
errorbar=("ci", 95), ax=axes[1, 1])
axes[1, 1].set_title("barplot (mean + 95% CI)")
# (f) violin + swarm (layered)
sns.violinplot(data=df_cat, x="Treatment", y="Response",
palette=["#d1e5f0", "#d1e5f0", "#d1e5f0"],
inner=None, ax=axes[1, 2])
sns.swarmplot(data=df_cat, x="Treatment", y="Response",
palette=["#2166ac", "#67a9cf", "#ef8a62"],
size=3, ax=axes[1, 2])
axes[1, 2].set_title("violin + swarm overlay")
plt.suptitle("Seaborn Categorical Plots", fontsize=14, fontweight="bold", y=1.01)
plt.tight_layout()
plt.show()
print("→ Violin plots reveal distribution shape that box plots hide.")
print(" Swarm/strip overlays show individual observations.")
print(" Layering (box + strip, violin + swarm) gives the best of both worlds.")
Section 3.8: Pair Plots and Scatter Matrices¶
Once a dataset has several continuous variables, pairwise inspection becomes tedious if every scatter plot is drawn by hand. A pair plot automates that first multivariate scan. The diagonal panels show each variable's distribution. The off-diagonal panels show every pairwise relationship.
Pair plots are especially useful before modeling because they reveal separation, overlap, outliers, clusters, and nonlinear relationships. In the Iris dataset, for example, petal measurements separate one species almost immediately, while other species overlap more.
The cost is density. A pair plot grows quickly as the number of variables increases, so it is best used for a focused set of features rather than every column in a wide table. It is a map for exploration, not a final figure for every audience.
# === Section 3.8: Pair Plot ===
# Use the Iris dataset — the classic multivariate example
iris = sns.load_dataset("iris")
g = sns.pairplot(iris, hue="species",
palette={"setosa": "#2166ac", "versicolor": "#ef8a62", "virginica": "#b2df8a"},
diag_kind="kde",
plot_kws={"alpha": 0.6, "s": 30, "edgecolor": "white"},
height=2.5)
g.figure.suptitle("Pair Plot — Iris Dataset (3 Species)", y=1.02, fontsize=14, fontweight="bold")
plt.tight_layout()
plt.show()
print(f"→ Pair plot of {iris.shape[1]-1} continuous variables = {(iris.shape[1]-1)**2} panels.")
print(" Diagonal: KDE per species. Off-diagonal: scatter per pair.")
print(" Immediately reveals that petal_length separates setosa from the rest.")
Section 3.9: Plotly for Interactive Visualization¶
Static figures are essential, but interactivity can be valuable when exploration depends on hovering, zooming, filtering, trace toggling, or inspecting dense points. Plotly renders figures as browser-native HTML, which makes it useful in notebooks, dashboards, and web-facing reports.
Plotly Express is the high-level API. It works well with tidy DataFrames and can create common interactive charts with concise calls. Plotly Graph Objects is the lower-level API. It is more verbose but gives full control over traces, subplots, layout, and specialized figure types.
Interactivity should serve a purpose. Hover text is useful when individual observations matter. Zooming is useful when dense regions need inspection. Trace toggles are useful for subgroup comparisons. A 3D plot is useful only when the third dimension truly clarifies structure; otherwise a 2D plot with color or facets is often more readable.
The examples below introduce Plotly Express, Graph Objects, and interactive categorical plots. The public reader build now converts Plotly notebook MIME bundles into real HTML so these examples render as figures rather than dropped output.
# === Section 3.9a: Plotly Express — Quick Interactive Plots ===
# Scatter with color and size encoding — all from a DataFrame
fig_px = px.scatter(
df_clinic,
x="Age",
y="Systolic_BP",
color="Treatment",
size="BMI",
hover_data=["LDL_Cholesterol"],
title="Interactive Scatter — Age vs Systolic BP (hover for details)",
color_discrete_sequence=["#2166ac", "#ef8a62", "#b2df8a"],
template="plotly_white",
)
fig_px.update_layout(width=800, height=500)
fig_px.show()
print("→ Plotly Express: one line of code → fully interactive plot.")
print(" Hover over any point to see its BMI and LDL values.")
# === Section 3.9b: Plotly Graph Objects — Full Control ===
# 3-D scatter plot: Age × LDL × Systolic BP
color_map = {"Drug A": "#2166ac", "Drug B": "#ef8a62", "Placebo": "#b2df8a"}
fig_3d = go.Figure()
for treatment, group in df_clinic.groupby("Treatment"):
fig_3d.add_trace(go.Scatter3d(
x=group["Age"],
y=group["LDL_Cholesterol"],
z=group["Systolic_BP"],
mode="markers",
marker=dict(size=4, color=color_map[treatment], opacity=0.7),
name=treatment,
text=[f"Age: {a}, LDL: {l}, BP: {b}"
for a, l, b in zip(group["Age"], group["LDL_Cholesterol"], group["Systolic_BP"])],
hoverinfo="text+name",
))
fig_3d.update_layout(
title="3-D Scatter — Age × LDL × Systolic BP",
scene=dict(
xaxis_title="Age",
yaxis_title="LDL Cholesterol",
zaxis_title="Systolic BP",
),
width=800, height=600,
template="plotly_white",
)
fig_3d.show()
print("→ 3-D plots are powerful for exploring 3-variable relationships.")
print(" Drag to rotate, scroll to zoom, hover for values.")
print(" Use sparingly — 2-D with color encoding often communicates better.")
# === Section 3.9c: Plotly Express — Distribution & Categorical ===
fig = make_subplots(rows=1, cols=2,
subplot_titles=["Box Plot by Treatment",
"Violin Plot by Treatment"])
# Box
for i, (treatment, group) in enumerate(df_clinic.groupby("Treatment")):
fig.add_trace(
go.Box(y=group["Systolic_BP"], name=treatment,
marker_color=["#2166ac", "#ef8a62", "#b2df8a"][i]),
row=1, col=1)
# Violin
for i, (treatment, group) in enumerate(df_clinic.groupby("Treatment")):
fig.add_trace(
go.Violin(y=group["Systolic_BP"], name=treatment,
marker_color=["#2166ac", "#ef8a62", "#b2df8a"][i],
box_visible=True, meanline_visible=True,
showlegend=False),
row=1, col=2)
fig.update_layout(title_text="Interactive Box & Violin Plots (Plotly)",
width=900, height=450, template="plotly_white")
fig.show()
print("→ Plotly box and violin plots allow hovering over quartiles and means.")
print(" Great for presentations where the audience needs to explore the data.")
Section 3.10: Anscombe's Quartet¶
Anscombe's quartet is the classic warning against trusting summary statistics alone. The four datasets have nearly identical means, variances, correlations, and regression lines, yet their scatter plots show very different realities.
One dataset is approximately linear. One is curved. One is strongly affected by an outlier. One is dominated by a leverage point. The numerical summaries are the same enough to tempt a careless analyst, but the pictures make the differences obvious.
This lesson belongs in an AI/ML book because model evaluation can fall into the same trap. Two models can share an accuracy score while failing on different subgroups. Two features can share a correlation while having different nonlinear structure. Two datasets can share marginal summaries while having different joint distributions.
The rule is simple: compute summaries, but always plot the data.
# === Section 3.10: Anscombe's Quartet ===
anscombe = sns.load_dataset("anscombe")
fig, axes = plt.subplots(2, 2, figsize=(12, 10), sharex=False, sharey=False)
datasets = ["I", "II", "III", "IV"]
for ax, ds in zip(axes.flat, datasets):
subset = anscombe[anscombe["dataset"] == ds]
ax.scatter(subset["x"], subset["y"], color="steelblue", s=60, edgecolors="white", zorder=5)
# Regression line
slope, intercept, r, p, se = stats.linregress(subset["x"], subset["y"])
x_fit = np.linspace(subset["x"].min() - 1, subset["x"].max() + 1, 100)
ax.plot(x_fit, intercept + slope * x_fit, color="#ef8a62", linewidth=2, linestyle="--")
ax.set_title(f"Dataset {ds} (r = {r:.3f})", fontsize=12)
ax.set_xlabel("x")
ax.set_ylabel("y")
# Print stats in corner
ax.text(0.05, 0.92,
f"ȳ = {subset['y'].mean():.2f}\nŝ = {subset['y'].std():.2f}",
transform=ax.transAxes, fontsize=9, verticalalignment="top",
bbox=dict(boxstyle="round", facecolor="#d1e5f0", alpha=0.8))
plt.suptitle("Anscombe's Quartet — Same Statistics, Different Stories",
fontsize=14, fontweight="bold", y=1.01)
plt.tight_layout()
plt.show()
print("→ All four datasets have nearly identical: mean, variance, r, regression line.")
print(" Only visualization reveals the true structure:")
print(" I: linear, II: curved, III: outlier-driven, IV: leverage point.")
print("\n→ ALWAYS plot your data. Statistics without visualization is flying blind.")
Section 3.11: Multidimensional Visualization¶
Many real datasets cannot be understood one variable or one pair at a time. Multidimensional visualization tries to show structure across several variables without pretending that a two-dimensional page can show everything.
Heatmaps are useful for matrix-shaped summaries such as correlations, confusion matrices, missingness patterns, and expression grids. Parallel coordinates show each observation as a line crossing several feature axes, which can reveal group profiles after features are normalized. Radar charts compare a small number of groups across a small number of metrics, although they should be used sparingly because area and angle can distort perception.
The common risk is overload. Multidimensional plots can become visually impressive while becoming analytically unclear. Use them when they answer a specific question: which variables cluster, which group has a distinct profile, which feature separates classes, or which pattern deserves a more focused follow-up plot?
# === Section 3.11a: Heatmap — Correlation Matrix ===
# Compute correlation on the clinical data
numeric_cols = ["Age", "Systolic_BP", "LDL_Cholesterol", "BMI"]
corr = df_clinic[numeric_cols].corr()
fig, ax = plt.subplots(figsize=(7, 6))
mask = np.triu(np.ones_like(corr, dtype=bool), k=1) # upper triangle mask
sns.heatmap(corr, mask=mask, annot=True, fmt=".2f", cmap="RdBu_r",
vmin=-1, vmax=1, center=0, square=True,
linewidths=1, cbar_kws={"shrink": 0.8},
ax=ax)
ax.set_title("Correlation Heatmap (lower triangle)", fontsize=13)
plt.tight_layout()
plt.show()
print("→ Heatmaps with a triangular mask avoid redundancy (corr is symmetric).")
print(" RdBu_r diverging palette centers on 0: blue = positive, red = negative.")
# === Section 3.11b: Parallel Coordinates ===
# Normalize features to [0, 1] for comparison on a common scale
from sklearn.preprocessing import MinMaxScaler
iris = sns.load_dataset("iris")
features = ["sepal_length", "sepal_width", "petal_length", "petal_width"]
iris_scaled = iris.copy()
iris_scaled[features] = MinMaxScaler().fit_transform(iris[features])
fig_pc = px.parallel_coordinates(
iris_scaled,
dimensions=features,
color=iris["species"].map({"setosa": 0, "versicolor": 1, "virginica": 2}),
color_continuous_scale=["#2166ac", "#ef8a62", "#b2df8a"],
title="Parallel Coordinates — Iris (normalized 0–1)",
labels={f: f.replace("_", " ").title() for f in features},
)
fig_pc.update_layout(width=900, height=450, template="plotly_white")
fig_pc.show()
print("→ Parallel coordinates show how each species 'profiles' across all 4 features.")
print(" Setosa (blue) has distinctly low petal values — visible as a cluster of lines.")
# === Section 3.11c: Radar Chart (Spider Chart) ===
# Compare three treatment groups across multiple clinical metrics
metrics = ["Age", "Systolic_BP", "LDL_Cholesterol", "BMI", "Months_Enrolled"]
groups = df_clinic.groupby("Treatment")[metrics].mean()
# Normalize each metric to [0, 1] for radar comparability
groups_norm = (groups - groups.min()) / (groups.max() - groups.min())
categories = metrics
N = len(categories)
angles = np.linspace(0, 2 * np.pi, N, endpoint=False).tolist()
angles += angles[:1] # close the polygon
fig, ax = plt.subplots(figsize=(7, 7), subplot_kw=dict(polar=True))
colors = {"Drug A": "#2166ac", "Drug B": "#ef8a62", "Placebo": "#b2df8a"}
for treatment, row in groups_norm.iterrows():
values = row.tolist() + [row.tolist()[0]]
ax.plot(angles, values, "o-", linewidth=2, color=colors[treatment], label=treatment)
ax.fill(angles, values, alpha=0.15, color=colors[treatment])
ax.set_thetagrids(np.degrees(angles[:-1]), categories, fontsize=10)
ax.set_title("Radar Chart — Treatment Group Profiles", fontsize=13,
fontweight="bold", pad=20)
ax.legend(loc="upper right", bbox_to_anchor=(1.3, 1.1))
plt.tight_layout()
plt.show()
print("→ Radar charts compare profiles across a small number of metrics.")
print(" Each axis is one metric; the polygon shape shows the 'signature' of each group.")
print(" Use only when N_metrics ≤ 8 — beyond that, parallel coordinates work better.")
Section 3.12: Choosing the Right Chart¶
The best chart is chosen by the data structure and the question, not by habit.
For one quantitative variable, start with a histogram, density plot, ECDF, or box plot. For one categorical variable, start with counts or proportions. For a quantitative variable split by category, compare distributions with box plots, violin plots, strip plots, or layered variants. For two quantitative variables, use a scatter plot, possibly with color, facets, or trend lines. For time, use a line plot only when order and continuity are meaningful.
For many variables, start with pair plots, heatmaps, or dimensionality-reduction plots, then narrow to the relationships that matter. For interactive exploration, use Plotly when hover, zoom, or filtering helps the analysis. For publication, prefer static figures that make the intended conclusion clear without requiring interaction.
Several anti-patterns recur. Do not use pie charts for many categories. Do not use bar charts for distributions when a box plot or violin plot would show spread. Do not use 3D effects for decoration. Do not truncate axes in a way that exaggerates differences. Do not use color without explaining what it encodes. Do not let the chart look more certain than the data.
Section 3.13: Definitive Visualization Resources¶
Visualization libraries change, and the official references should remain part of the reader's workflow.
Use Matplotlib's usage guide, API reference, and gallery when you need precise control over figures, axes, styles, annotations, and file export.
Use Seaborn's tutorial, API reference, and example gallery when the task is statistical visualization from a DataFrame.
Use Plotly's Python documentation, Plotly Express guide, Graph Objects guide, and API reference when interactivity or browser-native output is part of the task.
Good external books and guides include Edward Tufte's work on visual explanation, William Cleveland's work on graphing data, Alberto Cairo's writing on truthful visualization, and the Python Graph Gallery for practical examples. These are not substitutes for judgment, but they are useful companions.
Section 3.14: Chapter Summary and Forward Link¶
This chapter turned descriptive statistics into visual reasoning. It began with Matplotlib because every static figure in the Python scientific stack ultimately depends on figures, axes, artists, layout, annotations, color, and export. It then moved to Seaborn, where DataFrame-aware statistical plotting makes exploratory analysis faster and more expressive. Finally, it introduced Plotly for interactive HTML figures, where hover, zoom, and trace controls can support exploration and public reader output.
The deeper lesson is chart choice. A line plot implies order. A histogram shows distribution shape. A scatter plot shows paired observations. A box plot compresses a distribution into quartiles and outliers. A violin plot adds distribution shape. A heatmap turns a matrix into a visual field. A pair plot scans many bivariate relationships. A Q-Q plot or ECDF tells a different story than a bar chart. No chart is neutral; each one makes some structure visible and some structure less visible.
The professional standard is honest visibility. Label axes. Show units. Use color intentionally. Preserve accessibility. Avoid decorative 3D. Do not let interactivity compensate for a weak analytic question. Save figures in formats appropriate to their destination. Most importantly, plot before modeling whenever the data structure could hide a problem.
Chapter 4 moves from description and visualization into probability and distributions. The figures learned here will become the visual language for density, mass, expectation, variance, sampling, and uncertainty.
Quick Reference¶
| Task | Static Python tool | Interactive Python tool |
|---|---|---|
| Line plot | ax.plot |
px.line |
| Bar chart | ax.bar, sns.barplot |
px.bar |
| Scatter plot | ax.scatter, sns.scatterplot |
px.scatter |
| Histogram | ax.hist, sns.histplot |
px.histogram |
| Box or violin plot | sns.boxplot, sns.violinplot |
px.box, px.violin |
| Pairwise feature scan | sns.pairplot |
filtered Plotly scatter views |
| Correlation matrix | sns.heatmap |
px.imshow |
| Multifeature profile | parallel coordinates, radar chart | px.parallel_coordinates |
| Export | fig.savefig |
fig.write_html, fig.write_image |
References¶
Hunter, J. D. (2007). Matplotlib: A 2D graphics environment. Computing in Science and Engineering, 9(3), 90-95.
Waskom, M. L. (2021). Seaborn: Statistical data visualization. Journal of Open Source Software, 6(60), 3021.
Plotly Technologies Inc. Plotly Python documentation.
Matplotlib Developers. Matplotlib user guide and gallery.
Seaborn Developers. Seaborn tutorial and example gallery.
Anscombe, F. J. (1973). Graphs in statistical analysis. The American Statistician, 27(1), 17-21.
Cairo, A. (2016). The Truthful Art: Data, Charts, and Maps for Communication. New Riders.
Tufte, E. R. (2001). The Visual Display of Quantitative Information (2nd ed.). Graphics Press.