Setting Up Seaborn
Install with pip and set a theme. Seaborn's defaults are already more beautiful than Matplotlib's.
# pip install seaborn
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="whitegrid", palette="husl")
# Seaborn ships with example datasets
tips = sns.load_dataset("tips")
print(tips.head())Distribution Plots
Visualise the distribution of a single variable.
tips = sns.load_dataset("tips")
# Histogram + KDE
sns.histplot(data=tips, x="total_bill", hue="sex",
kde=True, bins=20)
plt.title("Bill Distribution by Gender")
plt.show()
# KDE only
sns.kdeplot(data=tips, x="tip", fill=True)
plt.show()Categorical Plots – Boxplot and Violin
Compare distributions across categories.
tips = sns.load_dataset("tips")
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
# Boxplot
sns.boxplot(data=tips, x="day", y="total_bill",
hue="sex", ax=axes[0])
axes[0].set_title("Bill by Day and Gender")
# Violin plot (shows full distribution)
sns.violinplot(data=tips, x="day", y="tip", ax=axes[1])
axes[1].set_title("Tip Distribution by Day")
plt.tight_layout()
plt.show()Heatmaps – Correlation Matrices
Visualise correlations between all numerical variables at once.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
# Correlation matrix
corr = tips.select_dtypes("number").corr()
sns.heatmap(corr, annot=True, fmt=".2f",
cmap="coolwarm", vmin=-1, vmax=1,
square=True, linewidths=0.5)
plt.title("Correlation Heatmap")
plt.show()Seaborn: Statistical Plots in One Line
Seaborn is built on matplotlib but aimed at statistical visualization. It works directly with pandas DataFrames and produces attractive, informative charts with far less code — matplotlib would need many lines for the same result.
import seaborn as sns
# pass the DataFrame + column names — seaborn handles the rest
sns.scatterplot(data=df, x="height", y="weight", hue="sex")
sns.boxplot(data=df, x="dept", y="salary") # distribution per group
sns.heatmap(df.corr(), annot=True) # correlation matrix
| Plot | Reveals |
|---|---|
scatterplot + hue | relationship, colored by a category |
boxplot / violinplot | distribution & outliers per group |
heatmap | correlations / matrices |
pairplot | every variable vs every other |
Why it's productive: seaborn understands "tidy" DataFrames — you pass column names and it maps data to aesthetics (position, color via hue, size) automatically, including legends and axis labels. The hue parameter alone — splitting any plot by a category with color — would be tedious in raw matplotlib. Remember: seaborn returns matplotlib axes, so you can still fine-tune with plt/ax calls afterward and save with plt.savefig(). Use seaborn for exploration and statistical insight; drop to matplotlib for pixel-level control.
🏋️ Practical Exercise
Create statistical visualizations:
- Load Seaborn’s built-in
tipsdataset. - Plot the distribution of total bills with
sns.histplotorsns.kdeplot. - Compare a numeric value across categories with a boxplot.
- Draw a correlation heatmap of the numeric columns.
🔥 Challenge Exercise
Using a dataset of your choice (or Seaborn’s tips/penguins), build a small set of charts that tell a story: a distribution plot, a categorical comparison (box or violin), a relationship plot (sns.scatterplot with a hue), and a correlation heatmap. Apply a consistent theme with sns.set_theme() and add titles. Bonus: use sns.pairplot to view all pairwise relationships at once.
📋 Summary
- Seaborn is a high-level statistical visualization library built on Matplotlib.
- It produces attractive charts with minimal code and integrates directly with pandas DataFrames.
- Distribution plots (
histplot,kdeplot) show how values are spread. - Categorical plots (boxplot, violin) compare a metric across groups.
- Heatmaps visualize matrices such as correlation between columns.
- The
hueparameter adds a categorical dimension via color.
Interview Questions on Seaborn
- What is Seaborn and how does it relate to Matplotlib?
- When would you choose Seaborn over plain Matplotlib?
- What is a distribution plot and what does it show?
- What does a correlation heatmap visualize?
- What is the
hueparameter used for? - What is the difference between a boxplot and a violin plot?
- How does Seaborn integrate with pandas DataFrames?
Related Topics
FAQ
Seaborn is a higher-level wrapper around Matplotlib focused on statistical charts. It produces polished visuals with far less code and works directly with DataFrames, while Matplotlib gives you lower-level control over every detail.
hue parameter do? +It splits the data by a categorical column and colors the plot accordingly, letting you compare subgroups within one chart — for example, separating points by species or category with distinct colors.
A boxplot shows the median, quartiles, and outliers. A violin plot adds a mirrored density curve, so you also see the full shape of the distribution — useful for spotting multimodal data.
Yes. Seaborn returns Matplotlib Axes objects, so you can fine-tune titles, labels, limits, and styling with normal Matplotlib calls after creating the plot.

