Matplotlib Overview
Matplotlib is the foundation library for 2D plotting in Python: the engine that renders every chart in a Jupyter notebook, the one that seaborn, Pandas, and most visualization packages call under the hood. It is old, deeply documented, and remarkably broad - from a five-line line chart to a multi-panel scientific figure with precise control over every pixel. This tutorial covers it end to end: the object model, the chart types, customization, layout, and publication output.
The Object Hierarchy
Everything you draw lives inside a hierarchy that is worth learning once:
- Figure - the top-level window or canvas. It can hold one or more subplots, a colorbar, or text.
- Axes - one plotting area with its own x and y scales, ticks, labels, and title. A figure typically has several; each independent chart is a separate Axes.
- Artists - the individual drawn objects inside an Axes: lines, scatter points, bars, text, annotations.
A common source of confusion: in matplotlib, "axes" is one chart, not the plural of the drawing axes. When documentation says "the axes," it means one subplot.
Two Ways to Call It
Matplotlib has two interfaces over the same engine. The pyplot interface operates on the "current" figure and axes - plt.plot(...) draws on whatever is active - and is compact for quick plots. The object-oriented interface names the objects explicitly - fig, ax = plt.subplots(); ax.plot(...) - and is the style you want for anything with more than one chart, because it removes all ambiguity about where a line is drawn. The tutorial teaches the pyplot style first for brevity, then switches to the OO style where it pays off.
Backends
A backend decides how a figure is drawn and displayed: TkAgg for a desktop window, Agg for non-interactive rendering to a file (the default on headless servers), the inline/widget backends in Jupyter, and SVG/PDF backends for vector output. You usually never choose one - the environment does - but knowing the terms explains the errors ("canvas is already being drawn") and the differences between notebook and script output.
When Matplotlib Is the Right Tool
- Standard chart types (line, scatter, bar, histogram, box) with full customization control.
- Publication and reporting figures - precise fonts, sizes, and vector formats.
- Building blocks - custom artists and transforms for charts no higher-level library offers.
For styled, one-line exploratory charts, Pandas' built-in plotting (itself matplotlib under the hood) or seaborn's statistical plots may be faster; when you need control that those layers do not expose, you drop down to matplotlib and stay there.
Summary
Matplotlib is the general-purpose 2D plotting engine of the Python ecosystem: a Figure holds Axes, each Axes holds artists, and you can drive it through pyplot convenience calls or explicit object references. Learn the hierarchy and the two interfaces, and every chart in this tutorial is a combination of the same few primitives.