Jugal kishore

Jun 15, 2025 • 2 min read

📊 Mastering Data Visualization in Python: Matplotlib & Seaborn Guide

📊 Mastering Data Visualization in Python: Matplotlib & Seaborn Guide

Data visualization is one of the most crucial skills in data analysis, storytelling, and decision-making. With Python, two of the most popular libraries for visualization are Matplotlib and Seaborn. In this article, we’ll dive deep into:

  1. Getting Started with Matplotlib

  2. Exploring Seaborn for Statistical Plots

  3. Customizing Plots with Titles, Labels, Legends, and More


📌 1. Getting Started with Data Visualization in Python Using Matplotlib

🧠 What is Matplotlib?

Matplotlib is the foundation of data visualization in Python. It’s a low-level library used to create static, animated, and interactive visualizations.

📥 Installation

pip install matplotlib

✅ Basic Example: Line Plot

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

plt.plot(x, y)
plt.title("Basic Line Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.grid(True)
plt.show()

📈 Other Basic Plots:

  • Bar Chart:

categories = ['A', 'B', 'C']
values = [10, 15, 7]

plt.bar(categories, values)
plt.title("Bar Chart")
plt.show()
  • Scatter Plot:

plt.scatter(x, y, color='red')
plt.title("Scatter Plot")
plt.show()

🎨 2. Exploring Seaborn: Advanced Statistical Plots and Color Palettes

🧠 What is Seaborn?

Seaborn is built on top of Matplotlib and offers beautiful themes and high-level functions for statistical plots.

📥 Installation

pip install seaborn

✅ Basic Example: Using Seaborn

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

# Load sample dataset
data = sns.load_dataset("tips")

# Draw a scatter plot with regression line
sns.lmplot(x="total_bill", y="tip", data=data)
plt.title("Total Bill vs Tip")
plt.show()

📊 Useful Plots in Seaborn:

  • Box Plot:

sns.boxplot(x="day", y="total_bill", data=data)
plt.title("Box Plot of Total Bill by Day")
plt.show()
  • Violin Plot:

sns.violinplot(x="day", y="tip", data=data, palette="muted")
plt.title("Violin Plot of Tips by Day")
plt.show()
  • Heatmap:

corr = data.corr()
sns.heatmap(corr, annot=True, cmap="coolwarm")
plt.title("Correlation Heatmap")
plt.show()
  • Count Plot:

sns.countplot(x="sex", data=data)
plt.title("Count Plot by Gender")
plt.show()

🧩 3. Customizing Your Plots: Titles, Labels, Legends, and More

Whether you’re using Matplotlib or Seaborn, customization helps in making your plots more readable and presentation-ready.

📝 Titles and Axis Labels

plt.plot(x, y)
plt.title("Custom Line Plot", fontsize=16, color='blue')
plt.xlabel("X-axis Label", fontsize=12)
plt.ylabel("Y-axis Label", fontsize=12)
plt.show()

🧭 Adding Legends

plt.plot(x, y, label='Line 1', color='green')
plt.plot(x, [i*2 for i in y], label='Line 2', color='orange')
plt.legend(loc='upper left')
plt.title("Multiple Lines with Legend")
plt.show()

🎨 Using Custom Color Palettes in Seaborn

sns.set_palette("pastel")  # Try others like "dark", "colorblind", "Set2", etc.
sns.boxplot(x="day", y="total_bill", data=data)
plt.title("Boxplot with Custom Palette")
plt.show()

🧱 Subplots

fig, axes = plt.subplots(1, 2, figsize=(12, 5))

axes[0].plot(x, y)
axes[0].set_title("Left Plot")

axes[1].bar(categories, values)
axes[1].set_title("Right Plot")

plt.tight_layout()
plt.show()

🔚 Conclusion

Data visualization is not just about charts — it’s about communicating insights effectively. Matplotlib gives you full control over your plots, while Seaborn makes it easier to create visually appealing and statistically sound graphics.

So whether you're building a dashboard, analyzing trends, or creating reports, mastering these two libraries is a must!

Join Jugal on Peerlist!

Join amazing folks like Jugal and thousands of other people in tech.

Create Profile

Join with Jugal’s personal invite link.

0

0

0