📊 Visualization with Matplotlib & Seaborn
Data Science में सिर्फ data collect और clean करना काफी नहीं है। Data को effectively visualize करना भी उतना ही important है। Python में सबसे ज्यादा इस्तेमाल होने वाली दो libraries हैं: Matplotlib और Seaborn। इनका use करके हम complex datasets को आसानी से graphical form में present कर सकते हैं।
❓ Visualization क्यों ज़रूरी है?
- 👀 Data distribution और pattern समझने के लिए
- 📉 Outliers detect करने के लिए
- 📊 Multiple variables के बीच relationship देखने के लिए
- 🎯 Insights को easy और interactive form में present करने के लिए
📌 Matplotlib Basics
import matplotlib.pyplot as plt
# Data
x = [1,2,3,4,5]
y = [2,4,6,8,10]
# Line Plot
plt.plot(x, y)
plt.title("Line Plot Example")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
# Bar Plot
plt.bar(x, y)
plt.title("Bar Plot Example")
plt.show()
# Pie Chart
plt.pie([30,40,20], labels=["A","B","C"], autopct="%1.1f%%")
plt.title("Pie Chart Example")
plt.show()
🌊 Seaborn Basics
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
# Sample Data
df = sns.load_dataset("tips")
# Histogram
sns.histplot(df["total_bill"], bins=20, kde=True)
plt.show()
# Boxplot
sns.boxplot(x="day", y="total_bill", data=df)
plt.show()
# Scatterplot
sns.scatterplot(x="total_bill", y="tip", hue="sex", data=df)
plt.show()
# Heatmap
sns.heatmap(df.corr(), annot=True, cmap="coolwarm")
plt.show()
⚖️ Matplotlib vs Seaborn
| Aspect | Matplotlib | Seaborn |
|---|---|---|
| Complexity | Low-level, more customization | High-level, easy syntax |
| Best for | Basic plots (line, bar, pie) | Statistical plots (box, violin, heatmap) |
| Customization | Very high | Medium (built-in themes) |
📈 Visualization Types
- 📍 Line Plot — Trends over time
- 📍 Bar Plot — Category wise comparison
- 📍 Pie Chart — Percentage distribution
- 📍 Histogram — Frequency distribution
- 📍 Boxplot — Outliers और spread
- 📍 Scatterplot — Relationship between two variables
- 📍 Heatmap — Correlation matrix
🌍 Real-Life Applications
- 🏥 Healthcare: रोगियों के data patterns visualize करना।
- 💹 Finance: Stock trends और correlation study करना।
- 🛒 E-commerce: User behavior और product popularity दिखाना।
- 🎬 Entertainment: Audience rating patterns analyze करना।
📝 Practice Assignments
- Matplotlib का use करके Students के marks का bar chart बनाइए।
- Seaborn से Titanic dataset पर Age distribution का histogram बनाइए।
- Scatterplot बनाइए (seaborn) जिसमें total_bill vs tip हो और gender के हिसाब से color-coded हो।
- Correlation heatmap बनाइए किसी dataset (Iris या Titanic) पर।
🏆 निष्कर्ष
Data visualization data science pipeline का सबसे attractive और useful step है। Matplotlib आपको low-level flexibility देता है, वहीं Seaborn statistical visualizations को आसान बनाता है। दोनों को साथ में use करने से आप powerful और beautiful data stories बना सकते हैं।