Ad – 728Γ—90
πŸ“Š Data Science

Matplotlib Tutorial – Data Visualisation with Python

Matplotlib is Python's foundational plotting library. It can produce publication-quality charts of virtually any type. It forms the base for higher-level libraries like Seaborn, Pandas plotting, and others. Learning Matplotlib fundamentals gives you full control over every visual element.

⏱️ 25 min read🎯 AdvancedπŸ“… Updated 2026

Line Plot

The most basic chart type β€” ideal for trends over time.

Python
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)

plt.figure(figsize=(10, 4))
plt.plot(x, y, color="blue", linewidth=2, label="sin(x)")
plt.plot(x, np.cos(x), color="red", linestyle="--", label="cos(x)")
plt.title("Sine and Cosine")
plt.xlabel("x")
plt.ylabel("y")
plt.legend()
plt.grid(True, alpha=0.3)
plt.savefig("waves.png", dpi=150)
plt.show()

Bar Chart

Compare values across discrete categories.

Python
categories = ["Python", "JavaScript", "Java", "C++", "Go"]
popularity = [30, 25, 20, 10, 5]
colors = ["#3776AB", "#F7DF1E", "#5382A1", "#00599C", "#00ADD8"]

plt.figure(figsize=(8, 5))
bars = plt.bar(categories, popularity, color=colors, edgecolor="white")

# Add value labels on bars
for bar, val in zip(bars, popularity):
    plt.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 0.5,
             f"{val}%", ha="center", fontweight="bold")

plt.title("Programming Language Popularity")
plt.ylabel("Popularity (%)")
plt.tight_layout()
plt.show()

Scatter Plot

Show relationships between two numerical variables.

Python
import numpy as np

np.random.seed(42)
x = np.random.randn(100)
y = 2 * x + np.random.randn(100) * 0.5
colors_arr = np.random.rand(100)

plt.figure(figsize=(7, 5))
plt.scatter(x, y, c=colors_arr, cmap="viridis",
            s=50, alpha=0.7, edgecolors="none")
plt.colorbar(label="Random category")
plt.title("Scatter Plot with Colour Mapping")
plt.xlabel("X variable")
plt.ylabel("Y variable")
plt.show()
Ad – 336Γ—280

Subplots – Multiple Charts

Display multiple charts side by side with subplots().

Python
fig, axes = plt.subplots(1, 2, figsize=(12, 5))

# Chart 1 - Left
axes[0].plot([1,2,3,4], [1,4,9,16], "b-o")
axes[0].set_title("Quadratic")
axes[0].set_xlabel("x")
axes[0].set_ylabel("xΒ²")

# Chart 2 - Right
data = np.random.normal(0, 1, 1000)
axes[1].hist(data, bins=30, color="steelblue", edgecolor="white")
axes[1].set_title("Normal Distribution")

plt.tight_layout()
plt.savefig("subplots.png")
plt.show()