Data Visualization I: Matplotlib Fundamentals
Data Science Fundamentals
Chapter 7 · Data Visualization I: Matplotlib Fundamentals
ds1-6 built a shared statistical vocabulary — mean, distribution, correlation — entirely in words and numbers. This chapter starts turning that vocabulary into pictures. Matplotlib is the foundational plotting library the entire Python data-visualization ecosystem, including ds1-8's own Seaborn, is built directly on top of.
The Figure/Axes Model
Matplotlib's core object hierarchy has two parts worth knowing by name: a Figure is the whole canvas — the entire window or image being produced. An Axes is one individual plot area within that figure, with its own x-axis, y-axis, and content. A figure can hold one Axes, or several arranged in a grid.
import matplotlib.pyplot as plt
fig, ax = plt.subplots() # one Figure, one Axes
ax.plot(df["date"], df["revenue"])
ax.set_title("Daily Revenue")
ax.set_xlabel("Date")
ax.set_ylabel("Revenue ($)")
plt.show()
plt.plot(...) directly, with no explicit figure or axes) also exists and still works — but it implicitly tracks "the current figure" behind the scenes, which gets confusing fast once more than one chart is involved. Explicitly naming fig and ax scales cleanly to multiple subplots and is the style used consistently through the rest of this course.
Four Chart Types, Four Jobs
Line Plot
ax.plot(x, y) — trends over an ordered sequence, almost always time. A natural fit for the coffee-shop dataset's own date column, now a real datetime thanks to ds1-4.
Bar Plot
ax.bar(categories, values) — comparing discrete categories side by side. The direct visual counterpart to ds1-5's own groupby results (revenue per product, revenue per store).
Scatter Plot
ax.scatter(x, y) — the relationship between two continuous variables. This is ds1-6's own promised payoff: "the standard way to actually see a correlation visually."
Histogram
ax.hist(values, bins=...) — the actual shape of a distribution, bucketed into ranges. Turns ds1-6's own mean/standard-deviation description of a normal curve into something actually seen, not just described.
Line Plot — Revenue Over Time
daily = df.groupby("date")["revenue"].sum()
fig, ax = plt.subplots()
ax.plot(daily.index, daily.values)
ax.set_title("Daily Revenue Over Time")
Bar Plot — Revenue by Product
by_product = df.groupby("product")["revenue"].sum()
fig, ax = plt.subplots()
ax.bar(by_product.index, by_product.values)
ax.set_title("Total Revenue by Product")
Scatter Plot — Quantity vs. Revenue
fig, ax = plt.subplots()
ax.scatter(df["quantity"], df["revenue"])
ax.set_xlabel("Quantity")
ax.set_ylabel("Revenue ($)")
A visibly upward-sloping cluster of points here would be the visual signature of a strong positive correlation — ds1-6's own Pearson coefficient close to 1. ds1-6's own confounding-variable warning still applies at full strength here: a clear pattern in a scatter plot is a reason to investigate, never proof of a direct causal link on its own.
Histogram — The Shape of Order Revenue
fig, ax = plt.subplots()
ax.hist(df["revenue"], bins=10)
ax.set_title("Distribution of Order Revenue")
This is the chart that would actually make ds1-6's own catering-order outlier visible at a glance — a tall cluster of ordinary-sized orders, with one isolated bar sitting far off to the right.
Choosing the Right Chart Type
| Question being asked | Chart | Delivers on |
|---|---|---|
| How does this change over time? | Line | — |
| How do these categories compare? | Bar | ds1-5's own groupby results |
| Are these two variables related? | Scatter | ds1-6's own correlation coefficient |
| What's the overall shape of this variable? | Histogram | ds1-6's own distribution/normal-curve material |
ds1-1's own "Communicate" stage of the workflow exists to prevent.
Hands-On Exercises
Explain the difference between a Figure and an Axes in this chapter's own terms, and explain why this course's own fig, ax = plt.subplots() style scales better to multiple charts than the older plt.plot() style.
📄 View solutionUsing this chapter's own chart-type table, explain which chart type you'd choose to compare total revenue across the two coffee shop stores, and which chart type you'd choose to check whether order quantity and revenue move together — and why each choice fits ds1-6's own vocabulary.
📄 View solutionExplain why this chapter says a clear upward pattern in a scatter plot is "a reason to investigate, never proof of a direct causal link," tying your answer back to ds1-6's own confounding-variable material.
📄 View solutionChapter 7 Quick Reference
- Figure — the whole canvas · Axes — one plot area within it;
fig, ax = plt.subplots()is this course's own consistent style - Line — trend over time · Bar — compare categories (ds1-5's groupby) · Scatter — relationship between two variables (ds1-6's correlation) · Histogram — shape of a distribution (ds1-6's own normal-curve material)
- A scatter plot's own visible pattern is never proof of causation on its own — ds1-6's confounding-variable warning still applies
- Unlabeled charts undermine ds1-1's own "Communicate" stage of the workflow
- Next chapter: Data Visualization II: Seaborn & Statistical Plots