Skip to content

Seaborn

Install packages

You can use both pip or astral/uv.

1
2
3
4
5
6
!uv pip install -q \
    pandas==2.3.2 \
    pandas-stubs==2.3.2.250827 \
    numpy==2.3.2 \
    matplotlib==3.10.6 \
    seaborn==0.13.2

Import packages

1
2
3
4
5
6
7
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

sns.set_style("darkgrid")
sns.set_theme(style="darkgrid")

Usage

Plots

Load tips dataset.

1
2
3
tips = sns.load_dataset("tips")

tips.head()
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
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4

Scatter Plot

1
2
3
4
sns.scatterplot(x="total_bill", y="tip", data=tips, color="#F79E6B")

plt.title("Total bill x tips")
plt.show()
output_10_0.png

Line Plot

1
2
3
4
sns.lineplot(x="size", y="total_bill", data=tips, color="#92A78C")

plt.title("Total bill by size")
plt.show()
output_12_0.png

Bar Plot

sns.barplot(
    x="day",
    y="total_bill",
    data=tips,
    color="#F7CD82",
    estimator=np.mean,
)

plt.title("Average bill per day")
plt.show()
output_14_0.png

Box Plot

1
2
3
4
sns.boxplot(x="day", y="total_bill", data=tips, color="#E0D5AD")

plt.title("Total bill per day")
plt.show()
output_16_0.png

Violin Plot

1
2
3
4
sns.violinplot(x="day", y="total_bill", data=tips, color="#F7CD82")

plt.title("Total bill per day")
plt.show()
output_18_0.png

Histogram Plot

1
2
3
sns.histplot(tips["total_bill"].tolist(), bins=10, kde=True)

plt.show()
output_20_0.png

KDE Plot

sns.kdeplot(tips["total_bill"].tolist(), fill=True)
plt.show()
output_22_0.png

Pair Plot

sns.pairplot(tips)
plt.show()
output_24_0.png

Heat Map

correlation = tips[["total_bill", "tip", "size"]].corr()
correlation
total_bill tip size
total_bill 1.000000 0.675734 0.598315
tip 0.675734 1.000000 0.489299
size 0.598315 0.489299 1.000000

sns.heatmap(correlation, annot=True, cmap="coolwarm")
plt.show()
output_27_0.png

Customization

plt.figure(figsize=(10, 5))


sns.barplot(
    x="day",
    y="total_bill",
    data=tips,
    color="#F7CD82",
    estimator=sum,
)

plt.ylabel("Total bill")
plt.xlabel("Day")
plt.title("Total bills per day")
plt.show()
output_29_0.png