Also known as: plt, pyplot
matplotlib is Python's most widely used plotting library, providing a comprehensive toolkit for creating static, animated, and interactive visualisations. Its core module, matplotlib.pyplot (conventionally imported as plt), provides a MATLAB-like interface for quick plotting: plt.plot(x, y), plt.scatter(x, y), plt.hist(data), plt.bar(labels, values), and many more.
matplotlib follows a hierarchical object model: a Figure contains one or more Axes (individual plots), each of which contains Artists (lines, text, patches, images). The pyplot interface creates and manages these objects implicitly, while the object-oriented interface (fig, ax = plt.subplots()) gives you explicit control. The OO interface is preferred for complex plots, multi-panel figures, and production code.
matplotlib's strength is its flexibility: virtually every element of a plot can be customised. It supports dozens of plot types, multiple backends (Qt, Tk, web), export to many formats (PNG, PDF, SVG), and integration with Jupyter notebooks (inline display). Higher-level libraries like seaborn (statistical visualisation) and plotly (interactive web plots) are built on or complement matplotlib. pandas DataFrames integrate directly via .plot() methods that delegate to matplotlib.
Discussed in:
- Chapter 16: Working with Data — Visualisation with matplotlib