matplotlib

**Matplotlib: Python Plotting Foundation** **Overview** Matplotlib is the grandfather of Python visualization. Founded in 2003, it mimics MATLAB's plotting interface. While verbose and sometimes "ugly" by default, it is the most powerful and flexible plotting library available. **Architecture** - **Backend Layer**: Rendering to PNG, PDF, SVG, or GUI window. - **Artist Layer**: Primitives (Line2D, Rectangle, Text). - **Scripting Layer (pyplot)**: The user API (`plt.plot`). **Anatomy of a Figure** - **Figure**: The whole window/page. - **Axes**: The plot itself (x-axis, y-axis). *Note: "Axes" != "Axis".* - **Axis**: The number lines. **Basic Usage** ```python import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) y = np.sin(x) plt.figure(figsize=(10, 6)) plt.plot(x, y, label="Sin Wave", color="red", linestyle="--") plt.title("My Plot") plt.xlabel("Time") plt.ylabel("Amplitude") plt.legend() plt.grid(True) plt.show() ``` **Subplots** Creating multiple plots in one image. ```python fig, ax = plt.subplots(2, 1) # 2 rows, 1 col ax[0].plot(x, y) ax[1].plot(x, np.cos(x)) ``` **Modern Usage** Most people use wrappers *around* Matplotlib for quick plotting: - **Pandas**: `df.plot()` - **Seaborn**: `sns.lineplot()` But understanding Matplotlib is essential for tweaking the final output (font sizes, tick labels, annotations) for publication.

Go deeper with CFSGPT

Get AI-powered deep-dives, save terms, and run advanced simulations — free account.

Create Free Account