Matplotlib Plot Types

All examples assume fig, ax = plt.subplots().

๐Ÿ“ˆ Line plot

ax.plot(x, y)
ax.plot(x, y, color="red", linestyle="--", marker="o", linewidth=2, label="series 1")
ax.plot(x, y1, x, y2)          # multiple lines, one call

๐Ÿ”ต Scatter plot

ax.scatter(x, y)
ax.scatter(x, y, c=colors, s=sizes, alpha=0.6, cmap="viridis")   # c=color values, s=marker size

๐Ÿ“Š Bar chart

ax.bar(categories, values)                # vertical
ax.barh(categories, values)                   # horizontal
ax.bar(x, values, width=0.4, color="steelblue")
 
# grouped bars
ax.bar(x - 0.2, values1, width=0.4, label="A")
ax.bar(x + 0.2, values2, width=0.4, label="B")
 
# stacked bars
ax.bar(x, values1, label="A")
ax.bar(x, values2, bottom=values1, label="B")

๐Ÿ“‰ Histogram

ax.hist(data, bins=20)
ax.hist(data, bins=20, density=True, alpha=0.6, edgecolor="black")
ax.hist([data1, data2], bins=20, label=["A", "B"])   # multiple datasets

๐Ÿฅง Pie chart

ax.pie(sizes, labels=labels, autopct="%1.1f%%", startangle=90)
ax.pie(sizes, explode=[0.1, 0, 0, 0], shadow=True)   # "explode" one slice out

Pie charts are generally discouraged for >5 categories โ€” hard to compare slice sizes visually. Bar chart is usually clearer.

๐Ÿ“ฆ Box plot

ax.boxplot(data)                          # data = list of arrays, one box per array
ax.boxplot(data, labels=["A", "B", "C"], vert=True, showmeans=True)

๐ŸŽป Violin plot

ax.violinplot(data)

๐Ÿ—บ๏ธ Area plot

ax.fill_between(x, y1, y2, alpha=0.3)          # shade region between two curves
ax.stackplot(x, y1, y2, y3, labels=["A","B","C"])   # stacked area

๐ŸŒก๏ธ Heatmap / 2D data

ax.imshow(matrix, cmap="viridis")
ax.pcolormesh(X, Y, Z, cmap="coolwarm")

See Matplotlib Colors & Colormaps for colorbar setup.

๐Ÿ”บ Error bars

ax.errorbar(x, y, yerr=errors, fmt="o", capsize=5)

๐Ÿ“ Step plot

ax.step(x, y, where="mid")   # "pre", "post", or "mid"

๐Ÿงฎ Quick decision table

Data shapePlot type
Trend over continuous xplot (line)
Two continuous variables, relationshipscatter
Comparing categoriesbar / barh
Distribution of one variablehist
Distribution across groupsboxplot / violinplot
Part-to-whole (few categories)pie
2D grid / matrix dataimshow / pcolormesh

๐Ÿ”— Next

Matplotlib Styling & Customization ยท Matplotlib Colors & Colormaps