Lesson 25 of 30
Data Visualisation with Matplotlib
Line charts, bar charts, histograms, scatter plots, and customising figures.
Installing Matplotlib
pip install matplotlib
Line Chart
import matplotlib.pyplot as plt
years = [2020, 2021, 2022, 2023, 2024]
revenue = [15, 22, 31, 28, 40]
plt.figure(figsize=(8, 4))
plt.plot(years, revenue, marker="o", color="#3fb950")
plt.title("Annual Revenue ($M)")
plt.xlabel("Year")
plt.ylabel("Revenue")
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
Bar Chart
subjects = ["Math", "Science", "English"]
scores = [85, 92, 78]
plt.bar(subjects, scores, color=["#58a6ff", "#3fb950", "#f78166"])
plt.title("Subject Scores")
plt.ylim(0, 100)
plt.show()
Scatter Plot and Histogram
import numpy as np
x = np.random.randn(200)
y = x * 2 + np.random.randn(200)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))
ax1.scatter(x, y, alpha=0.5)
ax1.set_title("Scatter Plot")
ax2.hist(x, bins=20, color="#e3b341")
ax2.set_title("Histogram")
plt.tight_layout()
plt.show()