Advertisement
📊 Data Science

Seaborn Tutorial – Statistical Data Visualisation in Python

Seaborn is a statistical data visualisation library built on top of Matplotlib. It produces beautiful charts with minimal code, integrates seamlessly with Pandas DataFrames, and provides specialised statistical plots — distribution plots, categorical plots, regression plots, and heatmaps — that Matplotlib lacks.

⏱️ 22 min read🎯 Advanced📅 Updated 2026

Setting Up Seaborn

Install with pip and set a theme. Seaborn's defaults are already more beautiful than Matplotlib's.

Python
# 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())
▶ 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

Distribution Plots

Visualise the distribution of a single variable.

Python
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.

Python
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()
Advertisement

Heatmaps – Correlation Matrices

Visualise correlations between all numerical variables at once.

Python
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
PlotReveals
scatterplot + huerelationship, colored by a category
boxplot / violinplotdistribution & outliers per group
heatmapcorrelations / matrices
pairplotevery 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:

  1. Load Seaborn’s built-in tips dataset.
  2. Plot the distribution of total bills with sns.histplot or sns.kdeplot.
  3. Compare a numeric value across categories with a boxplot.
  4. 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 hue parameter 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 hue parameter used for?
  • What is the difference between a boxplot and a violin plot?
  • How does Seaborn integrate with pandas DataFrames?

FAQ

How is Seaborn different from Matplotlib? +

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.

What does the 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.

What is the difference between a boxplot and a violin plot? +

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.

Can I customize Seaborn plots with Matplotlib? +

Yes. Seaborn returns Matplotlib Axes objects, so you can fine-tune titles, labels, limits, and styling with normal Matplotlib calls after creating the plot.