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. Created by John D. Hunter in 2003, it provides a MATLAB-like interface for creating static, animated, and interactive visualisations. While there are many visualisation libraries in Python (Seaborn, Plotly, Altair), Matplotlib is the one they are all built on — understanding it gives you complete control over your plots.
Matplotlib offers two ways to create plots:
import matplotlib.pyplot as plt
import numpy as np
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
plt.title('Simple Line Plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('Simple Line Plot')
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
plt.show()
The object-oriented interface gives you more control and is the recommended approach for anything beyond quick plots.
Every Matplotlib plot has two key objects:
# Single plot
fig, ax = plt.subplots(figsize=(10, 6))
# Multiple subplots (2 rows, 2 columns)
fig, axes = plt.subplots(2, 2, figsize=(12, 8))
axes[0, 0].plot(x, y) # Top-left
axes[0, 1].bar(x, y) # Top-right
axes[1, 0].scatter(x, y) # Bottom-left
axes[1, 1].hist(y) # Bottom-right
plt.tight_layout() # Prevent overlap
plt.show()
Line plots are ideal for showing trends over time:
x = np.linspace(0, 10, 100)
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(x, np.sin(x), label='sin(x)', color='blue', linewidth=2)
ax.plot(x, np.cos(x), label='cos(x)', color='red', linestyle='--', linewidth=2)
ax.set_title('Trigonometric Functions', fontsize=16)
ax.set_xlabel('x', fontsize=12)
ax.set_ylabel('y', fontsize=12)
ax.legend(fontsize=12)
ax.grid(True, alpha=0.3)
plt.show()
| Style | Description |
|---|---|
'-' | Solid line |
'--' | Dashed line |
'-.' | Dash-dot line |
':' | Dotted line |
'o' | Circle marker |
's' | Square marker |
'^' | Triangle marker |
'x' | X marker |
Bar charts compare categories:
categories = ['Python', 'JavaScript', 'Java', 'C++', 'Go']
values = [85, 72, 65, 45, 38]
fig, ax = plt.subplots(figsize=(10, 6))
bars = ax.bar(categories, values, color=['#3776AB', '#F7DF1E', '#ED8B00', '#00599C', '#00ADD8'])
ax.set_title('Programming Language Popularity', fontsize=16)
ax.set_ylabel('Popularity Score', fontsize=12)
# Add value labels on bars
for bar, value in zip(bars, values):
ax.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 1,
str(value), ha='center', fontsize=11)
plt.show()
fig, ax = plt.subplots(figsize=(10, 6))
ax.barh(categories, values, color='steelblue')
ax.set_xlabel('Popularity Score')
ax.set_title('Programming Language Popularity')
plt.show()
x = np.arange(len(categories))
width = 0.35
fig, ax = plt.subplots(figsize=(10, 6))
ax.bar(x - width/2, values, width, label='2024')
ax.bar(x + width/2, [80, 75, 60, 50, 45], width, label='2025')
ax.set_xticks(x)
ax.set_xticklabels(categories)
ax.legend()
ax.set_title('Language Popularity Over Time')
plt.show()
Scatter plots show relationships between two variables:
np.random.seed(42)
x = np.random.randn(100)
y = 2 * x + np.random.randn(100) * 0.5
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.