Seaborn is a Python visualisation library built on Matplotlib. It works directly with pandas DataFrames and makes attractive statistical charts – distributions, comparisons and relationships – in one line each.
What You Will Learn
- Install and set a theme
- Distribution plots: histplot, kdeplot, boxplot, violinplot
- Category plots: barplot, countplot
- Relationship plots: scatterplot, regplot, lmplot, pairplot
- Heatmaps and saving figures
Prerequisites
EDA – examples use the cleaned emp.csv.
Setup
# pip install seaborn
import pandas as pd, seaborn as sns, matplotlib.pyplot as plt
df = pd.read_csv("emp.csv")
df = df[df.salary < 120850] # remove the error row and missing values
sns.set_theme(style="whitegrid")
Distributions
sns.histplot(data=df, x="salary", bins=20, kde=True)
plt.title("Salary distribution"); plt.show()
Explanation: A histogram with a smoothed KDE curve shows the shape of one numeric variable. Use hue="city" to overlay groups.
sns.boxplot(data=df, x="city", y="salary"); plt.show()
sns.violinplot(data=df, x="city", y="salary"); plt.show()
Explanation: Box plots show median, quartiles and outliers per group; violin plots add the full distribution shape.
Categories
sns.countplot(data=df, x="city"); plt.show()
sns.barplot(data=df, x="city", y="salary", estimator="median", errorbar=None); plt.show()
Explanation: countplot counts rows per category. barplot aggregates a numeric column (mean by default; here median). In Seaborn 0.12+ use errorbar=None instead of the old ci=None.
Relationships
sns.scatterplot(data=df, x="experience", y="salary", hue="city"); plt.show()
sns.regplot(data=df, x="experience", y="salary"); plt.show()
sns.lmplot(data=df, x="experience", y="salary", col="city"); plt.show()
Explanation: scatterplot colours points by group. regplot adds a regression line with a confidence band. lmplot draws one panel per city (a facet).
sns.pairplot(df, hue="city"); plt.show()
Explanation: pairplot draws every numeric pair as scatter plots and each variable’s distribution on the diagonal – a quick EDA overview.
Heatmap
corr = df[["age", "experience", "salary"]].corr()
sns.heatmap(corr, annot=True, cmap="coolwarm", vmin=-1, vmax=1); plt.show()
Explanation: Shows the correlation matrix with values printed. Fixing vmin/vmax to −1…1 keeps colours comparable across charts.
Save a Figure
ax = sns.boxplot(data=df, x="city", y="salary")
ax.set(xlabel="City", ylabel="Salary (₹)")
plt.tight_layout(); plt.savefig("salary_by_city.png", dpi=150)
Which Plot to Use
| Question | Plot |
|---|---|
| Shape of one variable | histplot, kdeplot |
| Compare groups | boxplot, violinplot, barplot |
| How many per category | countplot |
| Two numeric variables | scatterplot, regplot |
| Same chart per group | lmplot, relplot, catplot (col=…) |
| All pairs at once | pairplot |
| Matrix of values | heatmap |
| Seaborn vs Matplotlib | Seaborn | Matplotlib |
|---|---|---|
| Level | High-level, statistical | Low-level, full control |
| Input | DataFrames and column names | Arrays |
| Defaults | Polished themes | Plain |
| Use together? | Yes – Seaborn returns Matplotlib axes you can customise |
Series
Data Science series: What is Data Science · Lifecycle · Data Cleaning · EDA · Outliers · Feature Engineering · Visualization · Seaborn · Roadmap
Interview Questions
Q: Difference between barplot and countplot?
countplot counts rows; barplot aggregates a numeric y value per category.
Q: What does hue do?
Splits the data by a categorical column and colours each group differently.
Practice Exercises
- Draw a violin plot of salary by experience level (see Feature Engineering).
- Load sns.load_dataset(“tips”) and plot tip vs total_bill coloured by time.
Summary
Seaborn turns DataFrame columns into statistical charts with one call: hist/box/violin for distributions, bar/count for categories, scatter/reg/pair for relationships and heatmap for matrices.
Machine Learning Tutorials, Courses and Certifications