# Matplotlib Cheat Sheet: Plot Types and Common Commands

> Matplotlib cheat sheet with a downloadable one-page PNG: code for line, bar, scatter, histogram, heatmap and more, plus subplots, labels, ticks, legends, colors, and savefig.

Source: https://www.pythoncharts.com/matplotlib/matplotlib-cheat-sheet/ | Published: 2026-11-26 | Author: Alex Scott | Category: Matplotlib | Tags: matplotlib, cheat-sheet, plot types, subplots, savefig, python

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.

```python
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()
```

![Matplotlib line chart of monthly orders for two years with title, axis labels, gridlines, and legend](https://www.pythoncharts.com/matplotlib/matplotlib-cheat-sheet/images/matplotlib-cheat-sheet-quick-example.png)

## Download the Matplotlib Cheat Sheet

[Download the full-size cheat sheet (PNG)](https://www.pythoncharts.com/matplotlib/matplotlib-cheat-sheet/images/matplotlib-cheat-sheet.png). It's drawn entirely in Matplotlib, and the code on it targets version 3.11.

![One page Matplotlib cheat sheet with setup code, twelve chart types each shown with its code, and reference panels for figures, text, axes, legends, and saving](https://www.pythoncharts.com/matplotlib/matplotlib-cheat-sheet/images/matplotlib-cheat-sheet.png)

## 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.

```python
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](https://www.pythoncharts.com/matplotlib/matplotlib-line-plot/) | `ax.plot(x, y, "o-", markersize=4)` |
| [Scatter](https://www.pythoncharts.com/matplotlib/matplotlib-scatterplot/) | `ax.scatter(x, y, c=y, s=40)` |
| [Bar](https://www.pythoncharts.com/matplotlib/beautiful-bar-charts-matplotlib/) | `bars = ax.bar(cats, vals)`<br>`ax.bar_label(bars)` |
| [Horizontal bar](https://www.pythoncharts.com/matplotlib/matplotlib-horizontal-bar-chart/) | `ax.barh(cats, vals)`<br>`ax.invert_yaxis()` |
| [Histogram](https://www.pythoncharts.com/matplotlib/histograms/) | `ax.hist(data, bins=20, edgecolor="white")` |
| [Box plot](https://www.pythoncharts.com/matplotlib/box-plots-matplotlib/) | `ax.boxplot([data, 2 * data], tick_labels=["a", "b"])` |
| [Pie](https://www.pythoncharts.com/matplotlib/pie-chart-matplotlib/) | `ax.pie(vals, labels=cats, autopct="%.0f%%")` |
| [Error bars](https://www.pythoncharts.com/matplotlib/matplotlib-error-bars/) | `ax.errorbar(x, y, yerr=0.4, fmt="o", capsize=3)` |
| [Filled area](https://www.pythoncharts.com/matplotlib/matplotlib-gradient-fill-under-line/) | `ax.fill_between(x, y - 0.5, y + 0.5, alpha=0.3)`<br>`ax.plot(x, y)` |
| [Stacked area](https://www.pythoncharts.com/matplotlib/matplotlib-area-chart/) | `ax.stackplot(x, y + 1, 0.3 * x + 1)` |
| [Heatmap](https://www.pythoncharts.com/matplotlib/heatmaps-matplotlib/) | `im = ax.imshow(Z, cmap="magma")`<br>`fig.colorbar(im, ax=ax)` |
| [Contour](https://www.pythoncharts.com/matplotlib/matplotlib-contour-plot/) | `cs = ax.contourf(Z, levels=8)`<br>`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](https://www.pythoncharts.com/matplotlib/matplotlib-subplots/), [figure size](https://www.pythoncharts.com/matplotlib/figure-size-matplotlib/), and [dual y-axes](https://www.pythoncharts.com/matplotlib/matplotlib-dual-y-axes-twinx/).

## 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](https://www.pythoncharts.com/matplotlib/matplotlib-title-subtitle/), [axis labels](https://www.pythoncharts.com/matplotlib/matplotlib-axis-labels/), and [annotating with arrows](https://www.pythoncharts.com/matplotlib/matplotlib-annotate-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](https://www.pythoncharts.com/matplotlib/matplotlib-axis-limits-xlim-ylim/), [log scales](https://www.pythoncharts.com/matplotlib/matplotlib-logarithmic-axis-scale/), and [tick frequency](https://www.pythoncharts.com/matplotlib/matplotlib-set-ticks/).

## 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](https://www.pythoncharts.com/matplotlib/matplotlib-savefig/) covers formats and DPI, and [legends outside the plot](https://www.pythoncharts.com/matplotlib/matplotlib-legend-outside/) 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](https://www.pythoncharts.com/matplotlib/matplotlib-markers/), [named colors](https://www.pythoncharts.com/matplotlib/matplotlib-colors-colormaps/), and [style sheets](https://www.pythoncharts.com/matplotlib/matplotlib-built-in-style-sheets/).

## Similar Topics

- [Matplotlib Line Plots](https://www.pythoncharts.com/matplotlib/matplotlib-line-plot/)
- [Matplotlib Subplots](https://www.pythoncharts.com/matplotlib/matplotlib-subplots/)
- [How to Save a Figure in Matplotlib with savefig](https://www.pythoncharts.com/matplotlib/matplotlib-savefig/)
