Matplotlib figure construction
Use the explicit object interface for reusable code:
Figure -> one or more Axes -> Axis objects and data/annotation Artists
pyplot may create the Figure and Axes, but pass ax through helpers rather
than relying on hidden current-figure state.
Workflow
- Define the perceptual question, audience, output dimensions, medium, units,
accessibility needs, and whether axes are shared.
- Inspect data shape, missing values, category order, datetime timezone,
uncertainty, and scale. Do not let implicit string categories or date parsing
silently determine order.
- Create
fig, ax = plt.subplots(...); use Axes methods for marks, labels,
limits, scales, annotations, and legends. Return Figure/Axes or save the
artifact; do not bury global display side effects inside helpers.
- Use data coordinates for values, axes coordinates for annotations tied to
the panel, and figure coordinates only for figure-level layout.
- Set layout and export deliberately. For automation, select a noninteractive
backend before importing pyplot, save with explicit size/DPI/format, and
close figures created in loops or tests.
- Verify semantics and rendering: series count, data values, labels, limits,
scale, legend, accessible colors, output dimensions, and nonempty image.
Invariants
- A Figure is the complete canvas; an Axes is one plotting region; an Axis owns
ticks and scale; almost every visible item is an Artist.
- Do not mix stateful current-Axes calls with explicit Axes ownership in a
reusable function.
- Do not truncate a quantitative bar baseline without explicit disclosure and
a defensible reason.
- Use one shared normalization for color values that must be comparable across
panels.
- Use Seaborn only as a statistical plotting layer when its aggregation and
uncertainty semantics are intended; verify the resulting Matplotlib objects.
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(7, 4), layout="constrained")
ax.plot(months, values, marker="o", label="Observed")
ax.set(title="Monthly throughput", xlabel="Month", ylabel="Orders")
ax.legend()
fig.savefig("throughput.svg", metadata={"Title": "Monthly throughput"})
plt.close(fig)
Read the object and coordinate model, chart and
scale decisions, and render verification.
1---2name: matplotlib-python3description: Use for writing, reviewing, debugging, or testing static Python visualization with Matplotlib, including Figure, Axes, Axis, Artist, transforms, layouts, dates, categorical scales, annotations, color normalization, image export, and headless rendering. Do not use for Plotly interactivity, Altair/Vega-Lite specifications, dashboard state, or analysis without a Matplotlib artifact.4---56# Matplotlib figure construction78Use the explicit object interface for reusable code:910```text11Figure -> one or more Axes -> Axis objects and data/annotation Artists12```1314`pyplot` may create the Figure and Axes, but pass `ax` through helpers rather15than relying on hidden current-figure state.1617## Workflow18191. Define the perceptual question, audience, output dimensions, medium, units,20 accessibility needs, and whether axes are shared.212. Inspect data shape, missing values, category order, datetime timezone,22 uncertainty, and scale. Do not let implicit string categories or date parsing23 silently determine order.243. Create `fig, ax = plt.subplots(...)`; use Axes methods for marks, labels,25 limits, scales, annotations, and legends. Return Figure/Axes or save the26 artifact; do not bury global display side effects inside helpers.274. Use data coordinates for values, axes coordinates for annotations tied to28 the panel, and figure coordinates only for figure-level layout.295. Set layout and export deliberately. For automation, select a noninteractive30 backend before importing pyplot, save with explicit size/DPI/format, and31 close figures created in loops or tests.326. Verify semantics and rendering: series count, data values, labels, limits,33 scale, legend, accessible colors, output dimensions, and nonempty image.3435## Invariants3637- A Figure is the complete canvas; an Axes is one plotting region; an Axis owns38 ticks and scale; almost every visible item is an Artist.39- Do not mix stateful current-Axes calls with explicit Axes ownership in a40 reusable function.41- Do not truncate a quantitative bar baseline without explicit disclosure and42 a defensible reason.43- Use one shared normalization for color values that must be comparable across44 panels.45- Use Seaborn only as a statistical plotting layer when its aggregation and46 uncertainty semantics are intended; verify the resulting Matplotlib objects.4748```python49import matplotlib.pyplot as plt5051fig, ax = plt.subplots(figsize=(7, 4), layout="constrained")52ax.plot(months, values, marker="o", label="Observed")53ax.set(title="Monthly throughput", xlabel="Month", ylabel="Orders")54ax.legend()55fig.savefig("throughput.svg", metadata={"Title": "Monthly throughput"})56plt.close(fig)57```5859Read [the object and coordinate model](references/object-model.md), [chart and60scale decisions](references/decisions.md), and [render verification](references/testing.md).