Module 2 · Core Visualization with Matplotlib

Section 1: Introduction to Matplotlib and Basic Plots

Architecture · Line Charts · Bar Charts · Scatter Plots · Histograms · Saving

🏗️ Matplotlib Architecture
  • Figure — the canvas (entire window)
  • Axes — individual plot area within the figure
  • fig, ax = plt.subplots(figsize=(8,5))
  • OO interface (ax.plot()) preferred over pyplot API
📈 Line Charts
  • ax.plot(x, y) — basic line chart
  • Styling: linewidth, linestyle, marker, color
  • Best for: trends over time, continuous data
  • Multiple lines by calling ax.plot() multiple times
📊 Bar Charts
  • ax.bar(categories, values) — vertical bars
  • ax.barh() — horizontal (better for long labels)
  • Adjust: width, color, edgecolor, alpha
  • Best for: comparing discrete categories
🔵 Scatter Plots
  • ax.scatter(x, y) — individual data points
  • Encode extra dims: c=color_array, s=size_array
  • Best for: correlation, clustering, outlier spotting
  • Add alpha for transparency on dense data
📉 Histograms & Saving
  • ax.hist(data, bins=20) — frequency distribution
  • Use density=True for proportion instead of count
  • Save: plt.savefig("chart.png", dpi=300, bbox_inches="tight")
  • Formats: PNG for reports, SVG/PDF for scalable graphics
🧪 Lab 1 — Basic Matplotlib Plots
  • Part A: Monthly sales trend line chart with markers
  • Part B: Product price vs. sales scatter plot
  • Part C: Comparative bar chart across categories
  • Part D: Customer age vs. purchase amount scatter