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:
Getting Started with Matplotlib
Exploring Seaborn for Statistical Plots
Customizing Plots with Titles, Labels, Legends, and More
Matplotlib is the foundation of data visualization in Python. It’s a low-level library used to create static, animated, and interactive visualizations.
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()
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()
Seaborn is built on top of Matplotlib and offers beautiful themes and high-level functions for statistical plots.
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()
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()
Whether you’re using Matplotlib or Seaborn, customization helps in making your plots more readable and presentation-ready.
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()
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 ProfileJoin with Jugal’s personal invite link.
0
0
0