Seaborn
What is Seaborn?
Seaborn is a high-level Python data visualization library built on top of Matplotlib and closely integrated with Pandas.
It simplifies creating complex statistical graphics by automating semantic and statistical aggregations.
Seaborn helps us turn a dataset into charts that answer business questions.
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
sns. It works together with Matplotlib and Pandas.Seaborn uses Matplotlib under the hood, so you still need
plt.show() to display plots.
Why Do We Need Seaborn?
Imagine a bank manager has thousands of customer forms. The manager wants to know:
- How many customers subscribed?
- Which occupations had higher subscription rates?
- What ages were commonly contacted?
- Do subscribers have different balance distributions?
- How do outcomes vary by contact method?
Reading thousands of rows makes these patterns difficult to see. Charts make them easier to investigate.
Loading Datasets
Seaborn comes with several built-in datasets that are perfect for practice.
# Load a built-in dataset
tips = sns.load_dataset("tips")
print(tips.head())
# Or use your own Pandas DataFrame
bank = pd.read_csv("bank_market.csv")
tips, iris, titanic, penguins, flights.You can also pass any Pandas DataFrame to Seaborn plotting functions.
Relational Plots (scatterplot & lineplot)
Used to show the relationship between two numerical variables.
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="time")
plt.title("Total Bill vs Tip")
plt.show()
sns.lineplot(data=tips, x="size", y="total_bill")
plt.show()
hue parameter automatically colors the points by a categorical variable.sns.lineplot is useful when you want to show trend across ordered values.
Distribution Plots
These plots help us understand the distribution of a single variable.
# Histogram + KDE
sns.histplot(data=tips, x="total_bill", kde=True)
plt.show()
# Box plot
sns.boxplot(data=tips, x="day", y="total_bill")
plt.show()
histplot with kde=True shows both the histogram bars and a smooth density curve.boxplot shows median, quartiles and possible outliers for each category.
Categorical Plots
Useful when one of the variables is categorical.
# Count plot
sns.countplot(data=tips, x="day", hue="sex")
plt.show()
# Bar plot (shows mean by default)
sns.barplot(data=tips, x="day", y="total_bill", hue="sex")
plt.show()
countplot shows the number of observations in each category.barplot shows the average (mean) of a numeric variable for each category.
Heatmaps
Heatmaps are excellent for showing correlation matrices.
# Correlation heatmap
corr = tips.corr(numeric_only=True)
sns.heatmap(corr, annot=True, cmap="coolwarm", fmt=".2f")
plt.title("Correlation Heatmap")
plt.show()
annot=True shows the correlation values on the heatmap.cmap="coolwarm" uses a red-blue color scale (red = strong positive, blue = strong negative).
Pair Plots
A pair plot shows relationships between all pairs of numerical variables in one figure.
sns.pairplot(tips, hue="sex")
plt.show()
The diagonal shows the distribution of each variable. The other cells show scatter plots between pairs of variables.
Themes and Styles
Seaborn makes it easy to change the overall look of your plots.
# Set a theme
sns.set_theme(style="whitegrid")
# Other styles: "darkgrid", "white", "dark", "ticks"
sns.set_style("darkgrid")
# Change palette
sns.set_palette("pastel")
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="time")
plt.show()
"darkgrid", "whitegrid", "dark", "white", "ticks".Palettes control the color scheme used for categories (
"pastel", "deep", "muted", "bright", etc.).
Real Business Example (Bank Marketing)
Using the bank marketing dataset to answer business questions with Seaborn.
# How many customers subscribed?
sns.countplot(data=bank, x="y")
plt.title("Subscription Count")
plt.show()
# Subscription rate by job
sns.countplot(data=bank, y="job", hue="y",
order=bank["job"].value_counts().index)
plt.title("Subscription by Job")
plt.show()
# Age distribution of subscribers vs non-subscribers
sns.histplot(data=bank, x="age", hue="y", kde=True)
plt.title("Age Distribution by Subscription")
plt.show()
1. Overall subscription rate
2. Which jobs convert better
3. Whether age distribution differs between subscribers and non-subscribers
Seaborn — High-level Statistical Visualization
Create beautiful statistical charts easily with Pandas + Matplotlib power underneath.
Select any topic from the left sidebar to begin.
Start Learning →
