← 30 Days of Python
Day 17 / 30Visualisation

Matplotlib — Charts & Plots

Visualization is communication. Every insight you find means nothing until it is shown clearly. Matplotlib is the foundation — understand it and Seaborn becomes easy.

1
Easy

How do you create a basic line chart in Matplotlib?

Python Answer
import matplotlib.pyplot as plt

months = ["Jan", "Feb", "Mar", "Apr"]
sales = [120, 145, 132, 178]

plt.figure(figsize=(10, 5))
plt.plot(months, sales, marker="o", color="#FF6B00", linewidth=2)
plt.title("Monthly Sales 2026")
plt.xlabel("Month")
plt.ylabel("Sales (₹ thousands)")
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
💡

figsize=(width, height) in inches. marker="o" adds dots at data points. tight_layout() prevents labels from being cut off. Always label axes and add a title.

2
Easy

How do you create a bar chart?

Python Answer
regions = ["North", "South", "East", "West"]
revenue = [450, 380, 290, 520]

plt.figure(figsize=(8, 5))
plt.bar(regions, revenue, color=["#FF6B00","#1d4ed8","#16a34a","#7c3aed"])
plt.title("Revenue by Region")
plt.xlabel("Region")
plt.ylabel("Revenue (₹ lakhs)")
for i, v in enumerate(revenue):
    plt.text(i, v + 5, str(v), ha="center", fontweight="bold")
plt.show()
💡

Adding data labels with plt.text() is a professional touch — it eliminates the need for readers to read the Y-axis for each bar. ha="center" horizontally centers the label.

3
Easy

How do you create a histogram?

Python Answer
import numpy as np

data = df["salary"].dropna()

plt.figure(figsize=(9, 5))
plt.hist(data, bins=20, color="#1d4ed8", edgecolor="white", alpha=0.8)
plt.axvline(data.mean(), color="red", linestyle="--", label=f"Mean: {data.mean():,.0f}")
plt.title("Salary Distribution")
plt.xlabel("Salary (₹)")
plt.ylabel("Frequency")
plt.legend()
plt.show()
💡

bins controls the number of buckets. Adding a mean line with axvline() helps viewers immediately see where the average falls relative to the distribution. Histogram is essential for understanding numeric column distribution in EDA.

4
Easy

How do you create a scatter plot?

Python Answer
plt.figure(figsize=(8, 6))
plt.scatter(df["experience"], df["salary"],
            alpha=0.6, c=df["salary"], cmap="YlOrRd", s=60)
plt.colorbar(label="Salary")
plt.title("Experience vs Salary")
plt.xlabel("Years of Experience")
plt.ylabel("Salary (₹)")
plt.show()
💡

alpha controls transparency (useful when points overlap). c= maps a numeric column to color. s= controls point size. Scatter plots show correlation between two continuous variables.

5
Medium

How do you create subplots (multiple charts in one figure)?

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

# Left chart:
axes[0].bar(regions, revenue, color="#FF6B00")
axes[0].set_title("Revenue by Region")

# Right chart:
axes[1].plot(months, sales, marker="o")
axes[1].set_title("Monthly Sales Trend")

plt.tight_layout()
plt.show()
💡

subplots(rows, cols) returns a Figure and an array of Axes. Use axes[i] to target each chart. For a 2x2 grid, use axes[0,0], axes[0,1], etc. This is the standard way to build a multi-chart dashboard.

EVIKA ACADEMY · PYTHON FOR DATA ANALYTICS

Want to master Python with live practice?

Join our Python for Data Analysis course — live classes in Noida and online across India.

Book Free Demo Class →
← PREVIOUSDay 16: String & Date OperationsNEXT →Day 18: Seaborn — Statistical Visualisation
Best Data Analytics Course in Noida Delhi NCR | EVIKA ACADEMY