Matplotlib Beginner
~12 min read

First Steps with Matplotlib Plots

Matplotlib is the foundational plotting library in Python. Libraries like Seaborn and pandas plotting are built on top of it.

Line & Bar Charts

Matplotlib offers both a stateful interface via plt and an object‑oriented API using figure and axes objects. For quick experiments, the stateful style is fine; for dashboards and reusable plots, prefer fig, ax = plt.subplots() and call methods on ax for full control over each chart element.

import matplotlib.pyplot as plt

months = ["Jan", "Feb", "Mar", "Apr"]
sales = [100, 120, 90, 150]

plt.figure(figsize=(8, 4))
plt.plot(months, sales, marker="o", color="#e67e22")
plt.title("Monthly Sales")
plt.xlabel("Month")
plt.ylabel("Sales")
plt.grid(alpha=0.3)
plt.tight_layout()
plt.show()

Histograms

import numpy as np
import matplotlib.pyplot as plt

data = np.random.normal(loc=0, scale=1, size=1000)

plt.figure(figsize=(6, 4))
plt.hist(data, bins=30, color="#3498db", edgecolor="black", alpha=0.8)
plt.title("Histogram of Normal Data")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.tight_layout()
plt.show()