How to use the Matplotlib Cheat Sheet
- 1. Search or browseType what you want to do, or pick one section of the Matplotlib cheat sheet.
- 2. CopyEvery entry has its own Copy button, so it goes straight into your editor or notes.
- 3. DownloadSave the whole Matplotlib cheat sheet as plain text or Markdown for offline revision.
What's in it
Snippets use the object-oriented style: create fig, ax = plt.subplots() once and call methods on ax. Variables x, y and data stand for your own arrays. The 34 snippets are organised into 8 sections: Setup, Line & scatter, Bar & pie, Distributions, Labels & titles, Axes & ticks, Several plots, Style.
Every snippet was run with Matplotlib 3.x on sample data (non-interactive backend) without errors.
Example
A clean bar chart takes four lines: fig, ax = plt.subplots(figsize=(8, 4)), ax.bar(["A", "B", "C"], [5, 7, 3]), ax.set_title("Monthly sales"), then ax.spines[["top", "right"]].set_visible(False) to drop the box. Save it with fig.savefig("chart.png", dpi=200, bbox_inches="tight") so long labels aren't cut off.
Tips for using it
- Pass
label=on each series, then callax.legend(); a legend with no labels shows nothing. - Use barh for long category names instead of rotating labels by 90 degrees.
- In a loop that saves hundreds of charts, call plt.close(fig) after each save to keep memory flat.
A quick look, one entry per section
- Setup:
import matplotlib.pyplot as plt(the usual imports) - Line & scatter:
ax.plot(x, y, label="Sales")(line chart) - Bar & pie:
ax.bar(["A", "B", "C"], [5, 7, 3])(vertical bar chart) - Distributions:
ax.hist(data, bins=20, edgecolor="white")(histogram) - Labels & titles:
ax.set_title("Monthly sales")(title and axis labels) - Axes & ticks:
ax.set_xlim(0, 10)(set axis limits) - Several plots:
fig, axs = plt.subplots(2, 2, figsize=(8, 6))(a grid of plots; index axs[row, col]) - Style:
plt.style.use("ggplot")(use a built-in style (see plt.style.available))
Search the box above for the rest of the Matplotlib entries, or download the full sheet.
Frequently asked questions
Which Matplotlib version?
The snippets were run with Matplotlib 3.x. Passing a list to ax.spines needs 3.4 or later; set_xticks with labels= needs 3.5 or later.
Should I use seaborn instead?
Seaborn is built on Matplotlib and is quicker for statistical charts. You still use these Matplotlib calls to fine-tune titles, ticks and layout.
Is the Matplotlib cheat sheet free?
Yes. You can search, read and copy every entry in the Matplotlib cheat sheet without signing up. The first time you download it, we ask once for your name and WhatsApp number; after that downloads and every other Uncodemy tool open straight away.