Most Matplotlib charts follow one pattern: create a figure with fig, ax = plt.subplots(), draw with a method such as ax.plot() or ax.bar(), label it with ax.set(), and finish with plt.show() or fig.savefig(). This cheat sheet collects those commands on one page, with copyable tables below.
Quick Example
A complete chart with the parts you'll use most: two lines, a title, axis labels, ticks, gridlines, and a legend.
import matplotlib.pyplot as plt
import numpy as np
rng = np.random.default_rng(15)
month = np.arange(1, 13)
this_year = 40 + 3.2 * month + rng.normal(0, 3, 12)
last_year = 36 + 2.1 * month + rng.normal(0, 3, 12)
fig, ax = plt.subplots(figsize=(8, 4.5), layout="constrained")
ax.plot(month, this_year, "o-", color="#0f766e", label="This year")
ax.plot(month, last_year, "s--", color="#94a3b8", label="Last year")
ax.set_title("Monthly orders")
ax.set_xlabel("Month")
ax.set_ylabel("Orders")
ax.set_xticks(month)
ax.grid(alpha=0.3)
ax.legend()
plt.show()

Download the Matplotlib Cheat Sheet
Download the full-size cheat sheet (PNG). It's drawn entirely in Matplotlib, and the code on it targets version 3.11.

Setup Code for the Examples
The snippets in the next table use these variables. Run this once, then create fig, ax = plt.subplots() before each chart.
import matplotlib.pyplot as plt
import numpy as np
rng = np.random.default_rng(8)
x = np.linspace(0, 10, 25)
y = np.sin(x) + 0.2 * x
cats = ["A", "B", "C", "D"]
vals = [5, 8, 3, 6]
data = rng.normal(0, 1, 400)
Z = np.outer(np.hanning(20), np.hanning(30))
Matplotlib Plot Types
| Chart (links to guide) | Code |
|---|---|
| Line | ax.plot(x, y, "o-", markersize=4) |
| Scatter | ax.scatter(x, y, c=y, s=40) |
| Bar | bars = ax.bar(cats, vals)ax.bar_label(bars) |
| Horizontal bar | ax.barh(cats, vals)ax.invert_yaxis() |
| Histogram | ax.hist(data, bins=20, edgecolor="white") |
| Box plot | ax.boxplot([data, 2 * data], tick_labels=["a", "b"]) |
| Pie | ax.pie(vals, labels=cats, autopct="%.0f%%") |
| Error bars | ax.errorbar(x, y, yerr=0.4, fmt="o", capsize=3) |
| Filled area | ax.fill_between(x, y - 0.5, y + 0.5, alpha=0.3)ax.plot(x, y) |
| Stacked area | ax.stackplot(x, y + 1, 0.3 * x + 1) |
| Heatmap | im = ax.imshow(Z, cmap="magma")fig.colorbar(im, ax=ax) |
| Contour | cs = ax.contourf(Z, levels=8)fig.colorbar(cs, ax=ax) |
tick_labels in boxplot needs Matplotlib 3.9 or later; older versions call it labels.
pyplot or the Object-Oriented Interface?
| pyplot | Object-oriented | |
|---|---|---|
| Looks like | plt.plot(x, y) |
ax.plot(x, y) |
| Draws on | the current Axes | the Axes you name |
| Title | plt.title("t") |
ax.set_title("t") |
| x label | plt.xlabel("x") |
ax.set_xlabel("x") |
| x limits | plt.xlim(0, 10) |
ax.set_xlim(0, 10) |
| Good for | a quick single chart | subplots, functions, anything reused |
The tables on this page use the object-oriented form.
Figures, Subplots, and Layout
| Task | Code |
|---|---|
| One chart | fig, ax = plt.subplots(figsize=(8, 5)) |
| Grid of charts | fig, axs = plt.subplots(2, 3) |
| Uneven grid | fig, axd = plt.subplot_mosaic("AB;CC") |
| Second y-axis | ax2 = ax.twinx() |
| Auto spacing | fig.set_layout_engine("constrained") |
| Equal units | ax.set_aspect("equal") |
For more layouts, see Matplotlib subplots, figure size, and dual y-axes.
Titles, Labels, and Annotations
| Task | Code |
|---|---|
| Chart title | ax.set_title("Title") |
| Figure title | fig.suptitle("Title") |
| Axis labels | ax.set(xlabel="x", ylabel="y") |
| Free text | ax.text(2, 1, "note") |
| Arrow | ax.annotate("peak", xy=(2, 1), xytext=(4, 2), arrowprops={"arrowstyle": "->"}) |
Details are in the guides to titles, axis labels, and annotating with arrows.
Axis Limits, Scales, and Ticks
| Task | Code |
|---|---|
| Limits | ax.set_xlim(0, 10) |
| Log scale | ax.set_yscale("log") |
| Tick positions | ax.set_xticks([0, 5, 10]) |
| Rotate ticks | ax.tick_params(axis="x", rotation=45) |
| Gridlines | ax.grid(alpha=0.3) |
| Hide borders | ax.spines[["top", "right"]].set_visible(False) |
See axis limits, log scales, and tick frequency.
Legends, Reference Lines, and Saving
| Task | Code |
|---|---|
| Legend | ax.legend(loc="upper left") |
| Legend outside | ax.legend(loc="upper left", bbox_to_anchor=(1.02, 1)) |
| Reference line | ax.axhline(0, color="black") |
| Shaded span | ax.axvspan(2, 4, alpha=0.2) |
| Save | fig.savefig("plot.png", dpi=200, bbox_inches="tight") |
| Show | plt.show() |
Call savefig before plt.show(); in a script, the figure is gone once its window closes. The savefig guide covers formats and DPI, and legends outside the plot covers placement.
Line Styles, Markers, and Colors
| Setting | Common values |
|---|---|
linestyle |
"-" solid, "--" dashed, "-." dash-dot, ":" dotted, "none" |
marker |
"o" circle, "s" square, "^" triangle, "D" diamond, "x", "+", "*" star |
color by name |
"steelblue", "tab:orange", "C0" (first color in the cycle) |
color by value |
"#1f77b4", (0.12, 0.47, 0.71), "0.5" (gray) |
cmap |
"viridis", "magma" (sequential), "coolwarm", "RdBu" (diverging) |
| Style sheet | plt.style.use("ggplot"); list them with plt.style.available |
The fmt shorthand combines a color, marker, and line style in one string: ax.plot(x, y, "ro--") draws a red dashed line with circles. See all the marker styles, named colors, and style sheets.