Module 10 Lesson: Matplotlib and Visualization Foundations
Let us begin
A chart is not decoration. A chart is evidence.
Before building a chart, ask:
> What question should this chart answer?
If the chart does not answer a question or help check the data, it probably does not belong in the report.
Figure and axes
Matplotlib uses figures and axes.
- The figure is the whole canvas.
- The axes is the plotting area where data appears.
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(weekly["week"], weekly["total_minutes"])
ax.set_title("Weekly Practice Minutes")
ax.set_xlabel("Week")
ax.set_ylabel("Minutes")
plt.tight_layout()
This pattern gives you control.
Line chart
Use a line chart for ordered time or sequence.
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(weekly["week"], weekly["total_minutes"], marker="o")
ax.set_title("Weekly Practice Minutes")
ax.set_xlabel("Week")
ax.set_ylabel("Minutes")
Do not use a line chart for unordered categories.
Bar chart
Use a bar chart for category comparison.
fig, ax = plt.subplots(figsize=(8, 4))
ax.bar(course_summary["course_name"], course_summary["learner_count"])
ax.set_title("Learners by Course")
ax.set_xlabel("Course")
ax.set_ylabel("Learners")
plt.xticks(rotation=30, ha="right")
plt.tight_layout()
Bar charts need readable labels.
Scatter plot
Use a scatter plot to inspect a relationship between two numeric values.
fig, ax = plt.subplots(figsize=(6, 5))
ax.scatter(df["quiz_attempts"], df["practice_minutes"], alpha=0.7)
ax.set_title("Practice Minutes by Quiz Attempts")
ax.set_xlabel("Quiz attempts")
ax.set_ylabel("Practice minutes")
A scatter plot can suggest a relationship. It does not prove cause.
Histogram
Use a histogram to inspect distribution.
fig, ax = plt.subplots(figsize=(8, 4))
ax.hist(df["practice_minutes"].dropna(), bins=20)
ax.set_title("Distribution of Practice Minutes")
ax.set_xlabel("Practice minutes")
ax.set_ylabel("Learner count")
The number of bins affects the look. Try a few sensible values.
Box plot
Use a box plot to compare distributions across groups.
groups = [
group["practice_minutes"].dropna()
for _, group in df.groupby("status")
]
labels = [name for name, _ in df.groupby("status")]
fig, ax = plt.subplots(figsize=(7, 4))
ax.boxplot(groups, labels=labels)
ax.set_title("Practice Minutes by Status")
ax.set_xlabel("Status")
ax.set_ylabel("Practice minutes")
Box plots are compact but may need explanation for beginners.
Annotation
Use annotation to point to an important detail.
ax.annotate(
"Launch week",
xy=(launch_week, launch_minutes),
xytext=(launch_week, launch_minutes + 100),
arrowprops={"arrowstyle": "->"},
)
Annotate sparingly. Too many notes make a chart harder to read.
Export
fig.savefig("outputs/weekly_practice_minutes.png", dpi=150, bbox_inches="tight")
Use clear filenames.
Accessibility
For important charts, write a text equivalent.
Example:
> The line chart shows weekly practice minutes for eight weeks. Minutes rise from week 1 to week 3, dip in week 4, then stabilize. The chart shows recorded online practice only, not offline study.
Takeaway
Matplotlib gives you chart control. In the next module, Seaborn helps create statistical visuals more efficiently, especially for distributions, relationships, categories, and facets.
