You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Matplotlib is the foundational plotting library in Python. Nearly every other Python visualisation library (Seaborn, Pandas plotting, even some Plotly features) is built on top of or integrates with Matplotlib. Understanding its architecture gives you complete control over every element of a chart.
pip install matplotlib
The standard import convention:
import matplotlib.pyplot as plt
import numpy as np
Matplotlib offers two ways to create plots:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.title("Simple Line Plot")
plt.show()
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4], [10, 20, 25, 30])
ax.set_xlabel("X Axis")
ax.set_ylabel("Y Axis")
ax.set_title("Simple Line Plot")
plt.show()
Tip: Always use the object-oriented interface (
fig, ax = plt.subplots()) for anything beyond the simplest plot. It gives you explicit control over figures and axes.
| Object | Description |
|---|---|
| Figure | The overall window or canvas — can contain multiple Axes |
| Axes | A single plot area with its own x-axis, y-axis, title, and data |
| Axis | The x-axis or y-axis of an Axes object — controls ticks, labels, limits |
| Artist | Every visual element on a figure — lines, text, patches, etc. |
import matplotlib.pyplot as plt
# Create a figure with 1 row and 2 columns of Axes
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
ax1.bar(["A", "B", "C"], [10, 20, 15])
ax1.set_title("Bar Chart")
ax2.plot([1, 2, 3], [1, 4, 9], marker="o")
ax2.set_title("Line Plot")
fig.suptitle("Two Subplots", fontsize=14)
plt.tight_layout()
plt.show()
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
fig, ax = plt.subplots(figsize=(8, 5))
ax.plot(x, np.sin(x), label="sin(x)", linewidth=2)
ax.plot(x, np.cos(x), label="cos(x)", linewidth=2, linestyle="--")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_title("Trigonometric Functions")
ax.legend()
ax.grid(alpha=0.3)
plt.tight_layout()
plt.show()
import matplotlib.pyplot as plt
langs = ["Python", "R", "SQL", "Julia"]
users = [68, 18, 42, 5]
fig, ax = plt.subplots(figsize=(8, 5))
bars = ax.bar(langs, users, color=["#4C72B0", "#DD8452", "#55A868", "#C44E52"])
ax.set_ylabel("Usage (%)")
ax.set_title("Data Science Language Popularity")
ax.spines[["top", "right"]].set_visible(False)
# Add value labels on bars
for bar in bars:
height = bar.get_height()
ax.text(bar.get_x() + bar.get_width() / 2, height + 1,
str(height), ha="center", va="bottom")
plt.tight_layout()
plt.show()
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(42)
x = np.random.randn(200)
y = x + np.random.randn(200) * 0.5
colours = np.random.rand(200)
sizes = np.abs(np.random.randn(200)) * 100
fig, ax = plt.subplots(figsize=(8, 6))
scatter = ax.scatter(x, y, c=colours, s=sizes, alpha=0.6,
cmap="viridis", edgecolors="white")
fig.colorbar(scatter, ax=ax, label="Colour Value")
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_title("Scatter Plot with Colour and Size")
plt.tight_layout()
plt.show()
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(42)
data1 = np.random.normal(50, 10, 1000)
data2 = np.random.normal(60, 12, 1000)
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.