Skip to main content
Numbers alone rarely tell the whole story. Matplotlib is Python’s foundational plotting library — it gives you complete, fine-grained control over every element of a chart: axes, tick marks, colors, fonts, legends, and layout. Every other Python visualization library (Seaborn, Pandas plotting, Plotly) is built on or alongside Matplotlib, so learning it first means you always have an escape hatch when higher-level tools fall short.

Installation and Import

pyplot is the submodule you will use for almost everything. The alias plt is universal.

Two Interfaces: plt vs. Axes

Matplotlib offers two ways to build charts.
Call functions directly on plt. Matplotlib manages the current figure and axes automatically. Best for single charts.
Both produce identical output. Use the state-based style for quick single plots and the object-oriented style for anything with multiple panels.

Line Plot

Line plots show how a value changes over a continuous axis — most commonly time.

Common plot() Styling Options

Bar Chart

Bar charts compare quantities across discrete categories.

Horizontal Bar Chart

Use barh when category names are long — it prevents overlapping x-axis labels.

Scatter Plot

Scatter plots reveal correlations (or lack thereof) between two continuous variables.

Histogram

Histograms show the frequency distribution of a continuous variable by grouping values into “bins”.
The bins parameter controls granularity. Too few bins and the distribution looks flat; too many and it looks jagged. Start with 20–30 and adjust visually.

Customizing Charts

Every element of a Matplotlib chart is customizable.

Subplots — Multi-Panel Figures

Use plt.subplots(rows, cols) to create a grid of charts. Switch to the object-oriented interface to address each panel independently.
plt.tight_layout() automatically adjusts spacing so titles and labels do not overlap between subplots. Call it just before plt.show() or plt.savefig().

Saving Charts to File

Practical Example: Temperature Trend Analysis

Next: Seaborn

Build on Matplotlib with Seaborn’s statistical charts, beautiful defaults, and native Pandas DataFrame integration.

Matplotlib gallery

Browse hundreds of chart examples with full source code to find the right plot type for your data.