Matplotlib Tutorial
Visualize data with Matplotlib — line, bar, and scatter plots, plus styling.
Matplotlib is the foundation of Python plotting. Learn to make clear, labeled charts.
A first line plot
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [2, 4, 1, 8]
plt.plot(x, y, marker="o")
plt.title("Sample trend")
plt.xlabel("Day")
plt.ylabel("Value")
plt.show()
Bar and scatter
# Bar
plt.bar(["A", "B", "C"], [3, 7, 2], color="#2f6df6")
# Scatter
plt.scatter(x, y, c=y, cmap="viridis")
plt.colorbar()
Subplots
fig, axes = plt.subplots(1, 2, figsize=(10, 4))
axes[0].plot(x, y, marker="o")
axes[1].bar(["A", "B", "C"], [3, 7, 2])
fig.tight_layout()
plt.savefig("charts.png", dpi=150)
Styling for readability
plt.style.use("seaborn-v0_8-darkgrid")
plt.rcParams["font.size"] = 12
plt.rcParams["axes.spines.top"] = False
plt.rcParams["axes.spines.right"] = False
| Chart | Use when |
|---|---|
| Line | trends over time |
| Bar | comparing categories |
| Scatter | relationship between two variables |
Always label axes and add a title — an unlabeled chart is just decoration.