Tuesday, 21 October 2025

#3 Matplotlib + Seaborn Basics

๐Ÿ“Š Matplotlib & Seaborn Visualizations for Beginners

Welcome to your Matplotlib and Seaborn visual guide! Learn how to create awesome charts — from basic line plots to colorful heatmaps — all with a copy button and syntax-highlighted code.


1️⃣ Importing Libraries


import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

# Sample data
data = {
    'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
    'Sales': [200, 240, 300, 350, 400, 450]
}
df = pd.DataFrame(data)

2️⃣ Line Plot with Matplotlib


plt.figure(figsize=(8, 4))
plt.plot(df['Month'], df['Sales'], marker='o', color='cyan', linewidth=2)
plt.title('Monthly Sales Growth')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.grid(True)
plt.show()

3️⃣ Bar Chart


plt.figure(figsize=(7, 4))
plt.bar(df['Month'], df['Sales'], color='orange')
plt.title('Sales by Month')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.show()

4️⃣ Scatter Plot


import numpy as np

x = np.random.rand(50)
y = np.random.rand(50)
colors = np.random.rand(50)

plt.scatter(x, y, c=colors, cmap='viridis', s=100)
plt.title('Random Scatter Plot')
plt.show()

5️⃣ Pie Chart


sizes = [30, 25, 20, 15, 10]
labels = ['A', 'B', 'C', 'D', 'E']

plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140)
plt.title('Category Distribution')
plt.show()

6️⃣ Histogram


data = np.random.randn(1000)

plt.hist(data, bins=30, color='purple', edgecolor='white')
plt.title('Histogram of Random Data')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.show()

7️⃣ Seaborn — Basic Styling


sns.set_theme(style="darkgrid")

# Sample DataFrame
tips = sns.load_dataset('tips')
tips.head()

8️⃣ Seaborn — Histogram & KDE


sns.histplot(tips['total_bill'], bins=20, kde=True, color='skyblue')
plt.title('Total Bill Distribution')
plt.show()

9️⃣ Seaborn — Scatter & Regression


sns.lmplot(x='total_bill', y='tip', data=tips, hue='sex', height=5)
plt.title('Tips vs Total Bill')
plt.show()

๐Ÿ”Ÿ Seaborn — Heatmap


corr = tips.corr(numeric_only=True)
sns.heatmap(corr, annot=True, cmap='coolwarm')
plt.title('Correlation Heatmap')
plt.show()

๐Ÿ’ก Tip: Use plt.style.use('seaborn-v0_8-dark') to instantly switch to modern dark themes. Combine Matplotlib + Seaborn = Pro-level plots in Python! ๐ŸŽจ

No comments:

Post a Comment

#20b Python (run+edit in browser ^ all in one)

๐Ÿ Python Basics — 5 Interactive Modules Edit code live with syntax highlighting and click ▶ Run . Each module runs separately ...