Data Visualization: Matplotlib aur Seaborn

Lesson 14 of 26

Ek accha chart hazaar rows ki table se zyada baat samjha deta hai. Python me charts ke liye do main libraries hain: Matplotlib (base library, full control) aur Seaborn (Matplotlib ke upar bani, kam code me sundar statistical charts).

Line chart — time ke saath trend

Example

Python
import matplotlib.pyplot as plt

months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
sales = [120, 135, 128, 160, 175, 190]   # hazaar rupaye

plt.plot(months, sales, marker="o", color="#2563eb")
plt.title("Monthly Sales")
plt.xlabel("Month")
plt.ylabel("Sales (Rs '000)")
plt.grid(alpha=0.3)
plt.show()

Monthly sales ka line chart

Bar chart — categories ki tulna

Example

Python
categories = ["Electronics", "Clothing", "Grocery", "Books"]
revenue = [5400, 3200, 2100, 900]

plt.bar(categories, revenue, color="#f97316")
plt.title("Category wise Revenue")
plt.ylabel("Revenue (Rs)")
plt.show()

Category wise revenue ka bar chart

Histogram — distribution dekhna

Example

Python
import numpy as np

rng = np.random.default_rng(1)
marks = rng.normal(65, 12, size=500).clip(0, 100)

plt.hist(marks, bins=20, color="#10b981", edgecolor="white")
plt.axvline(marks.mean(), color="black", linestyle="--", label="Mean")
plt.title("Exam marks distribution")
plt.xlabel("Marks")
plt.legend()
plt.show()

Exam marks ka histogram

Scatter plot — do numbers ka rishta

Example

Python
experience = rng.integers(0, 16, size=100)
salary = 30000 + experience * 4000 + rng.normal(0, 6000, size=100)

plt.scatter(experience, salary, alpha=0.7, color="#8b5cf6")
plt.title("Experience vs Salary")
plt.xlabel("Experience (years)")
plt.ylabel("Salary (Rs)")
plt.show()

Experience vs salary ka scatter plot

Subplots — ek figure me kai charts

Example

Python
fig, axes = plt.subplots(1, 2, figsize=(10, 4))

axes[0].bar(categories, revenue, color="#f97316")
axes[0].set_title("Revenue")
axes[0].tick_params(axis="x", rotation=30)

axes[1].pie(revenue, labels=categories, autopct="%1.0f%%")
axes[1].set_title("Revenue share")

plt.tight_layout()
plt.show()

Bar chart aur pie chart side by side

Seaborn: boxplot

Seaborn seedha DataFrame leta hai — column ke naam do, chart ready:

Example

Python
import numpy as np
import pandas as pd

rng = np.random.default_rng(7)
n = 200
department = rng.choice(["IT", "Sales", "HR", "Finance"], size=n, p=[0.4, 0.3, 0.1, 0.2])
experience = rng.integers(0, 16, size=n)
education = rng.choice(["Graduate", "Post Graduate"], size=n, p=[0.65, 0.35])

base = {"IT": 45000, "Sales": 35000, "HR": 32000, "Finance": 42000}
salary = (np.array([base[d] for d in department])
          + experience * 4000
          + np.where(education == "Post Graduate", 8000, 0)
          + rng.normal(0, 6000, size=n)).round(-2)

emp = pd.DataFrame({"department": department, "experience": experience,
                    "education": education, "salary": salary})

import seaborn as sns

sns.boxplot(data=emp, x="department", y="salary", hue="education")
plt.title("Department aur Education ke hisaab se Salary")
plt.show()

Seaborn boxplot: department aur education ke hisaab se salary

Boxplot ki beech wali line median hai, box 25% se 75% tak ka data dikhata hai, aur bahar ke dots outliers hain.

Seaborn: correlation heatmap

Example

Python
emp["is_pg"] = (emp["education"] == "Post Graduate").astype(int)
corr = emp[["experience", "is_pg", "salary"]].corr()

sns.heatmap(corr, annot=True, fmt=".2f", cmap="coolwarm", vmin=-1, vmax=1)
plt.title("Correlation Heatmap")
plt.show()

Correlation heatmap

Kaunsa chart kab?

Sawaal

Chart

Time ke saath kya badla?

Line chart

Categories me kaun aage?

Bar chart

Values kaise faili hain?

Histogram, boxplot

Do numbers ka rishta?

Scatter plot

Groups ka spread aur outliers?

Boxplot

Kai columns ke correlations?

Heatmap

Hisse (share) kitne?

Pie chart — sirf 2-5 categories ke liye

Har chart pe title aur axis labels zaroor lagaiye. Bina label ka chart dekhne wale ke liye sirf rangeen lakeerein hai.