Menu
CoddyTech

Matplotlib Cheat Sheet

Last updated

Figure and axes

A figure is the whole image. An axes is one plot inside it, and almost every call below is a method on ax.

TaskCode
Import pyplotimport matplotlib.pyplot as plt
One figure with one plotfig, ax = plt.subplots()
Set the size in inchesfig, ax = plt.subplots(figsize=(8, 4))
Draw on the axesax.plot(x, y)
Show the figureplt.show()
Save as a sharp PNGfig.savefig("chart.png", dpi=150, bbox_inches="tight")
Save as vector graphicsfig.savefig("chart.svg"), fig.savefig("chart.pdf")
Transparent backgroundfig.savefig("chart.png", transparent=True)
Close a figure to free memoryplt.close(fig)
Quick pyplot style for one chartplt.plot(x, y) plt.title("Quick look") plt.show()

Plot types

TaskCode
Line chartax.plot(x, y)
Scatter plotax.scatter(x, y, s=20, alpha=0.7)
Scatter colored by value, with a color barpoints = ax.scatter(x, y, c=y, cmap="viridis") fig.colorbar(points, ax=ax)
Bar chartax.bar(cats, a)
Horizontal bar chartax.barh(cats, a)
Stacked barsax.bar(cats, a, label="2025") ax.bar(cats, b, bottom=a, label="2026")
Grouped barspos = np.arange(len(cats)) ax.bar(pos - 0.2, a, width=0.4) ax.bar(pos + 0.2, b, width=0.4) ax.set_xticks(pos, labels=cats)
Histogramax.hist(data, bins=30)
Box plotax.boxplot([data, data * 2], tick_labels=["A", "B"])
Violin plotax.violinplot([data, data * 2])
Pie chartax.pie([40, 35, 25], labels=cats, autopct="%1.0f%%")
Band between two curvesax.fill_between(x, y - 0.2, y + 0.2, alpha=0.3)
Error barsax.errorbar([1, 2, 3], [4, 5, 3], yerr=[0.5, 0.3, 0.6], fmt="o", capsize=4)
Step chartax.step(x, y, where="mid")
Heatmap of a 2D arrayimage = ax.imshow(np.random.default_rng(0).random((5, 5)), cmap="magma") fig.colorbar(image, ax=ax)
Filled contour plotX, Y = np.meshgrid(x, x) ax.contourf(X, Y, np.sin(X) * np.cos(Y), levels=20)
Density of many pointsax.hexbin(data, data + np.random.default_rng(1).normal(size=200), gridsize=20)
Horizontal or vertical reference lineax.axhline(0, color="gray", linestyle="--"), ax.axvline(5)
Shade a rangeax.axvspan(2, 4, alpha=0.2)

Lines, markers and colors

TaskCode
Color, width and line styleax.plot(x, y, color="tab:red", linewidth=2, linestyle="--")
Markers on the pointsax.plot(x, y, marker="o", markersize=4)
Short format stringax.plot(x, y, "g--o")
Transparencyax.plot(x, y, alpha=0.5)
Line styles"-", "--", "-.", ":"
Marker codes"o", "s", "^", "x", ".", "*"
Ways to name a color"tab:blue", "C1", "#1f77b4", (0.2, 0.4, 0.6)
n colors from a colormapcolors = plt.colormaps["viridis"](np.linspace(0, 1, 5))
Your own color cycleax.set_prop_cycle(color=["#0b7285", "#e8590c", "#5f3dc4"])
Outlined and hatched barsax.bar(cats, a, color="white", edgecolor="black", hatch="//")

Titles, labels and legends

TaskCode
Title and axis labelsax.set_title("Sales"), ax.set_xlabel("Month"), ax.set_ylabel("EUR")
All of them in one callax.set(title="Sales", xlabel="Month", ylabel="EUR")
Legend from line labelsax.plot(x, y, label="sin") ax.legend()
Legend position and columnsax.legend(loc="upper right", ncols=2, frameon=False)
Legend outside the plotax.legend(loc="center left", bbox_to_anchor=(1, 0.5))
Text at a data positionax.text(5, 0.5, "note")
Annotation with an arrowax.annotate("peak", xy=(1.57, 1), xytext=(3, 1.2), arrowprops=dict(arrowstyle="->"))
Value labels on barsbars = ax.bar(cats, a) ax.bar_label(bars)
Title for the whole figurefig.suptitle("Q1 report")
Math in labelsax.set_xlabel(r"$\alpha$ (rad)")

Axes, ticks and scales

TaskCode
Axis limitsax.set_xlim(0, 10), ax.set_ylim(bottom=0)
Log scaleax.set_yscale("log")
Choose ticks and their labelsax.set_xticks([0, 5, 10], labels=["start", "middle", "end"])
Rotate tick labelsax.tick_params(axis="x", labelrotation=45)
Grid linesax.grid(True, alpha=0.3)
Percent axisfrom matplotlib.ticker import PercentFormatter ax.yaxis.set_major_formatter(PercentFormatter(1.0))
Thousands separatorax.yaxis.set_major_formatter("{x:,.0f}")
Hide the top and right borderax.spines[["top", "right"]].set_visible(False)
Second y-axis on the rightax2 = ax.twinx() ax2.plot(x, y * 100, color="tab:orange")
Same scale on both axesax.set_aspect("equal")
Flip an axisax.invert_yaxis()
Hide the axes completelyax.axis("off")
Readable date labelsfig.autofmt_xdate()

Subplots and layout

plt.subplots(rows, cols) returns a NumPy array of axes. Index it like an array.

TaskCode
Grid of plotsfig, axs = plt.subplots(2, 2, figsize=(8, 6))
Draw on one of themaxs[0, 1].plot(x, y)
Loop over all of themfor ax in axs.flat: ax.grid(True)
Share an axis between plotsfig, axs = plt.subplots(1, 3, sharey=True)
Spacing that avoids overlapsfig, axs = plt.subplots(2, 2, layout="constrained")
Fix overlaps after drawingfig.tight_layout()
Columns of different widthsfig, axs = plt.subplots(1, 2, width_ratios=[3, 1])
Named layout with a wide panelfig, axd = plt.subplot_mosaic([["top", "top"], ["left", "right"]]) axd["top"].plot(x, y)
Small plot inside a plotinset = ax.inset_axes([0.6, 0.6, 0.35, 0.35])
Hide an empty panelaxs[0].axis("off")

Styles and defaults

TaskCode
Use a built-in styleplt.style.use("ggplot")
List the stylesplt.style.available
Seaborn look without seabornplt.style.use("seaborn-v0_8-whitegrid")
Style for one figure onlywith plt.style.context("dark_background"): fig, ax = plt.subplots()
Default font sizeplt.rcParams["font.size"] = 12
Default figure size and resolutionplt.rcParams["figure.figsize"] = (8, 4), plt.rcParams["figure.dpi"] = 120
Back to the defaultsplt.style.use("default")
Common colormaps: sequential, diverging, gray"viridis", "coolwarm", "Greys"

Plot from pandas

pandas plots with Matplotlib. Pass ax= to draw into your own axes, or keep the ax that pandas returns.

TaskCode
DataFrame columns into your axesdf.plot(x="month", y="sales", ax=ax)
Bar chart of a group summarydf.groupby("city")["sales"].sum().plot.bar(ax=ax)
Keep styling what pandas drewax = df.plot(x="month", y="sales") ax.set_title("Sales")
Columns straight into Matplotlibax.plot(df["month"], df["sales"])

Matplotlib 3.11: old forms and what to write now

Code copied from older tutorials can fail or warn in Matplotlib 3.11. Write the forms in the Matplotlib 3.11 column instead.

TaskMatplotlib 3.11Old form
Box plot labelsax.boxplot([data, data * 2], tick_labels=["A", "B"])labels=["A", "B"]
Horizontal box or violin plotax.boxplot(data, orientation="horizontal")vert=False
Get a colormapplt.colormaps["viridis"]plt.cm.get_cmap("viridis")
Seaborn styleplt.style.use("seaborn-v0_8")plt.style.use("seaborn")

The Matplotlib code you need to turn data into a clear chart, grouped by task: figure and axes, plot types, lines and colors, titles and legends, axes and ticks, subplots and layout, styles, and plotting from pandas. Each row is a short snippet you can paste, plus one line on what it does.

Everything here uses the object-oriented style (fig, ax = plt.subplots(), then methods on ax) and is written for Matplotlib 3.11, the version Coddy runs. Older forms such as labels= in boxplot appear only in the last table, next to what to write instead. The examples use x = np.linspace(0, 10, 50), y = np.sin(x) and a sample of normal values called data. New to Python itself? Start with the Python cheat sheet, and keep the pandas, NumPy and scikit-learn cheat sheets next to this one.

Matplotlib cheat sheet FAQ

Is this Matplotlib cheat sheet free?
Yes. This Matplotlib cheat sheet is completely free, with no sign-up required. Bookmark it, or print it to PDF from your browser, and come back whenever you need the exact call for a legend, a subplot grid or a color bar.
What is the difference between plt and ax?
plt.plot() draws on whatever axes is current, which is quick for a single chart. fig, ax = plt.subplots() gives you the figure and axes as objects, so ax.plot() always draws exactly where you mean. Use the ax style for anything with more than one plot, and for code you will keep.
How do I change the figure size?
Pass figsize in inches when you create the figure: fig, ax = plt.subplots(figsize=(10, 5)). To change the default for every figure, set plt.rcParams["figure.figsize"] = (10, 5). The pixel size of a saved file is figsize times dpi.
How do I save a chart in high resolution?
Call fig.savefig("chart.png", dpi=300, bbox_inches="tight"). A higher dpi gives a sharper PNG, and bbox_inches="tight" trims empty margins. For print or slides, save as SVG or PDF, which stay sharp at any size.
Why do my subplot titles and labels overlap?
Create the figure with layout="constrained", as in plt.subplots(2, 2, layout="constrained"), and Matplotlib makes room for titles, labels and color bars. On an existing figure, call fig.tight_layout() after drawing.
Which Matplotlib version does this cheat sheet cover?
Matplotlib 3.11, the version Coddy runs. Calls that newer versions removed or renamed, such as labels= in boxplot (now tick_labels=) and plt.cm.get_cmap (now plt.colormaps[...]), are in the last table next to what to write instead.
Can I run Matplotlib code online?
Yes. The free Matplotlib playground runs Python with Matplotlib, NumPy, pandas and scikit-learn in your browser and draws your figures next to the output, so you can paste any snippet from this cheat sheet and see the chart without installing anything. Coddy's Machine Learning course uses these plots to explore data before training a model.
Coddy programming languages illustration

Learn Matplotlib with Coddy

GET STARTED