Matplotlib Cheat Sheet

Matplotlib reference guide — line plots, scatter plots, bar charts, histograms, subplots, styling, and essential data visualization in Python.

Last Updated: July 15, 2025

Basic Plots

Plot TypeCode
Lineplt.plot(x, y)
Scatterplt.scatter(x, y, c=colors, s=sizes)
Barplt.bar(labels, values)
Histogramplt.hist(data, bins=20)
Pieplt.pie(values, labels=labels)

Customizing Plots

ElementCode
Titleplt.title("My Plot")
Labelsplt.xlabel("X"); plt.ylabel("Y")
Legendplt.legend(["Series A", "Series B"])
Colorsplt.plot(x, y, color='red', linestyle='--', marker='o')
Gridplt.grid(True, alpha=0.3)

Subplots

fig, axes = plt.subplots(2, 2, figsize=(10,8))
2x2 grid of subplots
axes[0,0].plot(x, y)
Plot in top-left subplot
plt.tight_layout()
Auto-adjust spacing between subplots

Saving & Display

ActionCode
Showplt.show()
Saveplt.savefig("plot.png", dpi=150, bbox_inches='tight')
Pro Tip: Always use plt.tight_layout() before saving — it prevents labels and titles from being cut off. For publication-quality figures, save at 300 DPI and use bbox_inches='tight' to trim whitespace.
← Back to Programming Languages | Browse all categories | View all cheat sheets