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.- State-based (plt.xxx) — simpler
- Object-oriented (fig, ax) — recommended
Call functions directly on
plt. Matplotlib manages the current figure and axes automatically. Best for single charts.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
Usebarh 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”.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
Useplt.subplots(rows, cols) to create a grid of charts. Switch to the object-oriented interface to address each panel independently.
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.