In the world of data science and machine learning, visualization is the bridge between raw numbers and meaningful insights. Among Python’s visualization tools, Matplotlib stands as the most widely used and versatile library. From quick exploratory data analysis plots to highly customized publication-ready graphs, Matplotlib provides the flexibility to do it all.
This Matplotlib Cheatsheet 2025 is designed as a beginner-to-advanced quick reference. Whether you’re just starting with your first line plot or working on advanced 3D visualizations and animations, this guide will give you all the essential functions, methods and examples you need in one place.
Table of Contents
1. Setup
import matplotlib.pyplot as plt
import numpy as np
2. Basic Plots
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y) # Line
plt.scatter(x, y) # Scatter
plt.bar([1,2,3],[4,5,6]) # Bar
plt.hist(np.random.randn(1000)) # Histogram
3. Extra Plot Types
Stem Plot
plt.stem(x, np.cos(x))
Step Plot
plt.step(x, y)
Error Bars
y_err = 0.2
plt.errorbar(x, y, yerr=y_err, fmt="o")
Heatmap (with imshow
)
data = np.random.rand(10,10)
plt.imshow(data, cmap="viridis", interpolation="nearest")
plt.colorbar()
Contour Plot
X, Y = np.meshgrid(x, x)
Z = np.sin(X) * np.cos(Y)
plt.contour(X, Y, Z, cmap="plasma")
plt.contourf(X, Y, Z, cmap="plasma")
Quiver Plot (arrows)
X, Y = np.meshgrid(np.arange(-5,5), np.arange(-5,5))
U, V = -1*X, Y
plt.quiver(X, Y, U, V)
Polar Plot
theta = np.linspace(0, 2*np.pi, 100)
r = np.abs(np.sin(5*theta))
plt.polar(theta, r)
4. Customization
Titles, Labels, Legends
plt.title("Sine Wave", fontsize=14, fontweight="bold")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.legend(["sin(x)"])
Axis Ticks & Labels
plt.xticks([0, 2, 4, 6, 8, 10], ["zero","two","four","six","eight","ten"])
plt.yticks(np.arange(-1, 1.5, 0.5))
Axis Formatting (Dates)
import matplotlib.dates as mdates
dates = np.arange("2025-01-01","2025-01-10",dtype="datetime64[D]")
values = np.random.rand(len(dates))
plt.plot(dates, values)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter("%b %d"))
plt.gcf().autofmt_xdate()
Axis Scales
plt.xscale("log")
plt.yscale("symlog")
5. Multiple Plots
Subplots
fig, axs = plt.subplots(2, 2, figsize=(8,6))
axs[0,0].plot(x, y)
axs[0,1].scatter(x, y)
axs[1,0].bar([1,2,3],[3,5,7])
axs[1,1].hist(np.random.randn(1000))
plt.tight_layout()
Twin Axes
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(x, y, "g-")
ax2.plot(x, np.cos(x), "b-")
Shared Axes
fig, axs = plt.subplots(2, sharex=True)
axs[0].plot(x, np.sin(x))
axs[1].plot(x, np.cos(x))
6. Advanced
Fill Between
plt.plot(x, y)
plt.fill_between(x, y, alpha=0.3)
Annotations
plt.annotate("Peak", xy=(np.pi/2,1), xytext=(2,1.5),
arrowprops=dict(facecolor="black", arrowstyle="->"))
Colormaps
plt.scatter(x, y, c=y, cmap="viridis")
plt.colorbar()
3D Plot
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
z = np.cos(x)
ax.plot(x, y, z)
Animation
import matplotlib.animation as animation
fig, ax = plt.subplots()
line, = ax.plot([], [])
def update(frame):
line.set_data(x[:frame], y[:frame])
return line,
ani = animation.FuncAnimation(fig, update, frames=len(x), interval=50)
7. Styling
Built-in Styles
plt.style.use("ggplot")
plt.plot(x, y)
Available Styles
print(plt.style.available)
8. Save Plots
plt.savefig("plot.png", dpi=300, bbox_inches="tight")
plt.savefig("plot.pdf")
Conclusion
Matplotlib may feel overwhelming at first due to its vast functionality, but once you understand its building blocks, it becomes an incredibly powerful tool. With this cheatsheet, you now have a complete reference covering everything from basic plots, customization, and subplots to heatmaps, animations, 3D graphics and advanced styling.
Use it as your go-to guide for creating clear, impactful, and professional visualizations in Python. Keep practicing with real datasets, and soon you’ll be able to bring any data story to life with Matplotlib.
Related Reads
- Scikit-learn Cheatsheet 2025: From Beginner to Advanced
- NumPy Cheatsheet 2025: From Basics to Advanced in One Guide
- Pandas Cheatsheet: The Ultimate Guide for Data Analysis in Python
- 10 Best AI Engineering Books to Read in 2025
- Mastering GPT-5 Prompting: The Complete Guide to Smarter AI Outputs