API Reference

Matplotlib

Comprehensive plotting library for Python that creates publication-quality static and interactive figures.

Official Documentation ↗
CompatibilityThis documentation covers version Matplotlib 3.8+ / Python 3.9+. Check the official docs for the latest API surface.

Installation

pippip install matplotlib

Methods

Overview

Matplotlib draws static, animated and interactive charts from Python data. It offers a quick pyplot interface and a fine-grained object API for controlling every element.

Core Methods

subplots creates a figure and axes, methods such as plot, scatter and hist draw data, and set and legend configure labels and legends. savefig writes the rendered result to a file.

Usage Notes

Use the object API in reusable code, pick the Agg backend on servers, and set layout options so labels are not clipped. Choose dpi and size deliberately for the target medium.

Common Pitfalls

Relying on the implicit pyplot state causes figures to leak between calls. Default fonts may lack glyphs for non-Latin text. Forgetting a non-interactive backend hangs in headless environments.

API reference evidence

Fetched from official sources on 2026-11-08T17:47:47+08:00 via WebFetch https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.subplots.html.

FAQ

What is the difference between pyplot and the object API?
pyplot manages a global current figure for quick scripts, while the figure and axes object API gives explicit control and is safer inside functions and libraries.
How do I set a headless backend?
Set matplotlib.use("Agg") before importing pyplot so figures render without a display, which is required on servers and CI.
Why do labels overlap or get cut off?
Layout is not automatic for every element. Use constrained_layout or tight_layout and bbox_inches="tight" on save to fit labels and titles inside the output.