Data Visualization with Matplotlib and Seaborn β Complete Guide
In this tutorial, you will learn data visualization with Python using Matplotlib and Seaborn to create publication-quality plots, customize styles, and communicate insights visually.
What You'll Learn
Build line charts, bar plots, scatter plots, heatmaps, and complex subplot layouts with Matplotlib and Seaborn using both pyplot and object-oriented interfaces.
Why It Matters
Visualization is the most effective way to find patterns and communicate findings. A well-designed chart reveals insights that summary statistics alone cannot show.
Real-World Use
A data analyst at a logistics company visualizes delivery times across regions, overlays traffic data, and identifies that 30 percent of delays occur in two specific zones, leading to reroutedι ι routes.
Visualization Decision Flow
flowchart TD
A[What is your data?] --> B{Numeric vs Categorical}
B -->|One Numeric| C[Histogram or Box Plot]
B -->|Two Numeric| D[Scatter Plot]
B -->|One Cat, One Num| E[Bar Chart or Box Plot]
B -->|Two Categorical| F[Count Plot or Heatmap]
B -->|Time Series| G[Line Chart]
C --> H[Seaborn: histplot / boxplot]
D --> I[Seaborn: scatterplot]
E --> J[Seaborn: barplot / boxplot]
F --> K[Seaborn: countplot / heatmap]
G --> L[Matplotlib: plot]
Setting Up Styles and Themes
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
sns.set_theme(style="whitegrid", palette="muted")
plt.rcParams.update({
"figure.figsize": (10, 6),
"font.size": 12,
"axes.titlesize": 14,
})
tips = sns.load_dataset("tips")
print(tips.head())
Output:
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
Multi-Panel Figures
fig, axes = plt.subplots(2, 2, figsize=(14, 10))
sns.histplot(tips["total_bill"], bins=30, kde=True, ax=axes[0, 0])
axes[0, 0].set_title("Total Bill Distribution")
sns.boxplot(x="day", y="total_bill", data=tips, ax=axes[0, 1])
axes[0, 1].set_title("Bill by Day")
sns.scatterplot(x="total_bill", y="tip", hue="time", data=tips, ax=axes[1, 0])
axes[1, 0].set_title("Tip vs Total Bill by Time")
sns.heatmap(tips.corr(numeric_only=True), annot=True, cmap="coolwarm",
center=0, ax=axes[1, 1])
axes[1, 1].set_title("Correlation Heatmap")
plt.tight_layout()
plt.show()
Output: A 2x2 grid showing histogram, box plot, scatter plot, and heatmap in one figure.
Customizing with the Object-Oriented API
fig, ax = plt.subplots(figsize=(10, 6))
sns.barplot(x="day", y="total_bill", hue="sex", data=tips,
estimator=np.mean, ci=95, ax=ax)
ax.set_title("Average Bill by Day and Gender", fontweight="bold")
ax.set_xlabel("Day of Week")
ax.set_ylabel("Average Total Bill ($)")
ax.legend(title="Gender")
sns.despine()
plt.show()
Output: A grouped bar chart showing average bills by day split by gender, with confidence interval whiskers and cleaned spines.
Practice Questions
- What are the advantages of using Seaborn over plain Matplotlib for statistical plots?
- How do you create a 2x3 grid of subplots and control individual axes?
- What is the purpose of
sns.despine()and when should you call it?
Answers:
- Seaborn provides built-in statistical aggregation, automatic grouping via hue, attractive default styles, and single-line complex plot functions.
- Use
plt.subplots(2, 3, figsize=(w, h))to get a 2D array of Axes objects and index likeaxes[row, col]to access each subplot. sns.despine()removes top and right spines for a cleaner look. Call it after plotting on the relevant axes or globally.
Challenge
Load the Iris dataset from Seaborn and create a figure with four panels: a scatter matrix (pairplot), a box plot of petal length by species, a violin plot of sepal width by species, and a heatmap of numeric correlations.
FAQs
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro