Module 2 · Core Visualization with Matplotlib

Section 3: Multiple Plots and Subplots

plt.subplots · Shared Axes · GridSpec · Dashboard Layouts · tight_layout

💼 Business Case for Subplots
  • Compare metrics side-by-side within a single figure
  • Tell a complete story without switching charts
  • Dashboard-style layouts for executive reports
  • Avoid clutter from multi-series single charts
🔲 plt.subplots()
  • fig, axes = plt.subplots(rows, cols, figsize=(w,h))
  • Access individual panels: axes[0, 1]
  • sharex=True / sharey=True for aligned scales
  • Flatten: axes.flatten() for loop iteration
📐 GridSpec Layouts
  • gs = fig.add_gridspec(2, 3) for flexible grids
  • Span rows/cols: fig.add_subplot(gs[0, :])
  • Mix wide overview charts with narrow detail panels
  • Set relative sizes with height_ratios, width_ratios
🔗 Shared & Linked Axes
  • Shared x-axis aligns time series across panels
  • Shared y-axis enables direct value comparison
  • Remove redundant tick labels on inner subplots
  • ax.set_visible(False) to hide empty panels
Finishing Touches
  • plt.tight_layout() — prevent label overlap
  • fig.suptitle() — overall figure title
  • plt.subplots_adjust(hspace, wspace) for spacing
  • Save at 300 DPI for presentation-quality output
🧪 Lab 3 — Subplot Dashboards
  • Part A: Regional performance 2×2 subplot dashboard
  • Part B: Product performance vertical 3-panel dashboard
  • Apply shared axes and suptitle across all panels
  • Export at 300 DPI for a boardroom-ready layout