Skip to content

Latest commit

 

History

History
115 lines (86 loc) · 5.12 KB

MatplotlibTutorial.md

File metadata and controls

115 lines (86 loc) · 5.12 KB

📊 Comprehensive Matplotlib Data Visualization & Headless Web Production Guide

Welcome to the definitive master tutorial guide on Matplotlib Data Visualization, Figure/Axes Architecture & Headless Web Integration.


📌 Table of Contents

  1. Executive Summary: Why Matplotlib Remains Essential
  2. The Object-Oriented (OOP) Pattern: Figure vs Axes
  3. Headless Server-Side Rendering (matplotlib.use('Agg'))
  4. In-Memory Base64 Serialization (Zero Disk Clutter)
  5. Gallery: From Simple Lines to 2x2 Dashboards
  6. Unit Testing & Production Verification
  7. Common Pitfalls & Best Practices

1. Executive Summary: Why Matplotlib Remains Essential

Matplotlib is the gold standard for 2D and 3D scientific visualization in Python. While high-level libraries like Seaborn, Plotly, and Altair exist, almost all of them depend on Matplotlib's underlying rendering pipeline.

Core Architecture Highlights:

  • Total Visual Control: Customize every pixel, spine, tick, marker, legend, and colormap.
  • Vector & Raster Output: Export to high-DPI PNGs, SVGs, and PDFs.
  • Server-Side Integration: Easily rendered inside Flask, FastAPI, and Django applications without requiring a desktop display server ($X11$ or Wayland).

2. The Object-Oriented (OOP) Pattern: Figure vs Axes

In professional codebases, never use the procedural plt.plot() state machine. Always use explicit OOP handles:

┌────────────────────────────────────────────────────────┐
│  Figure (The entire canvas / window container)         │
│  ┌────────────────────────┐  ┌──────────────────────┐  │
│  │  Axes 1 (Plot 1)       │  │  Axes 2 (Plot 2)     │  │
│  │  - Title               │  │  - Subplot title     │  │
│  │  - X/Y Labels & Ticks  │  │  - Histogram / Bars  │  │
│  │  - Lines / Curves      │  │  - Legend            │  │
│  └────────────────────────┘  └──────────────────────┘  │
└────────────────────────────────────────────────────────┘
# Preferred OOP Syntax:
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(x, y, color="#38bdf8", linewidth=2.0)
ax.set_title("Publication Figure", fontsize=12)
ax.set_xlabel("Time (s)")
ax.set_ylabel("Voltage (V)")
ax.grid(True)

3. Headless Server-Side Rendering (matplotlib.use('Agg'))

In web servers running with Gunicorn, uWSGI, or Docker containers, attempting to call plt.show() triggers GUI popups (Tkinter, Qt) which cause thread locks or hard crashes (TclError: no display name).

Golden Rule for Web Servers:

import os
os.environ.setdefault("MPLCONFIGDIR", "/tmp/matplotlib_cache")

import matplotlib
matplotlib.use("Agg")  # Non-interactive raster graphics backend
import matplotlib.pyplot as plt

4. In-Memory Base64 Serialization (Zero Disk Clutter)

Instead of saving .png files to disk (which causes file I/O latency, concurrency collisions, and disk cleanup headaches), encode the figure directly in memory:

import io
import base64

def get_plot_data_uri(fig: plt.Figure) -> str:
    buf = io.BytesIO()
    fig.savefig(buf, format="png", bbox_inches="tight", dpi=100)
    plt.close(fig)  # Critical: free figure memory!
    buf.seek(0)
    return base64.b64encode(buf.getvalue()).decode("utf-8")

In Jinja2 templates:

<img src="data:image/png;base64,{{ image_base64 }}" alt="Scientific Chart">

5. Gallery: From Simple Lines to 2x2 Dashboards

The Teach Cloud implementation in cloud_app/python/tutorials/machine_learning/matplotlib_basics.py demonstrates three master tiers:

  1. Damped Waveform (line_signal): Sinusoidal decay with max peak arrow annotation.
  2. Benchmark Bars (bar_benchmark): Inverted horizontal bars with custom categorical colors and inline latency metrics.
  3. 2x2 Analytics Dashboard (multiaxis_dashboard):
    • Panel 1: Gaussian Histogram with theoretical PDF curve
    • Panel 2: 2D Feature Correlation Heatmap (ax.imshow() with colorbar)
    • Panel 3: Scatter plot with dynamic linear regression line (np.polyfit)
    • Panel 4: Cumulative time-series area chart (ax.fill_between())

6. Unit Testing & Production Verification

Run the test suite to verify headless rendering and Base64 output:

pytest tests/test_matplotlib_tutorial.py -v

All tests verify non-GUI backend activation, image payload length (>1000 bytes), and multi-panel subplot integrity.