Seaborn turns a pandas DataFrame into a clear statistical chart in one line. This tutorial walks through the charts analysts use most, using a fictional food-delivery dataset from SpiceRoute with columns city, cuisine, amount, delivery_mins, distance_km, rating and order_date.
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="whitegrid")Distributions: what does one column look like?
sns.histplot(data=orders, x="delivery_mins", bins=30)
sns.histplot(data=orders, x="delivery_mins", hue="city", element="step", stat="density", common_norm=False)
sns.kdeplot(data=orders, x="amount", hue="city")Use these to see skew, outliers and whether groups differ. common_norm=False makes groups of different sizes comparable.
Box and violin plots: compare spread across groups
sns.boxplot(data=orders, x="city", y="delivery_mins")
sns.violinplot(data=orders, x="city", y="delivery_mins", inner="quartile")A box plot shows the median, the middle 50% and outliers. It is one of the best charts for comparing groups.
Counts and averages by category
sns.countplot(data=orders, y="cuisine", order=orders["cuisine"].value_counts().index)
sns.barplot(data=orders, x="city", y="amount", estimator="mean", errorbar=("ci", 95))countplot counts rows; barplot shows an average (or another estimator) with an uncertainty bar. Ordering bars by value makes them much easier to read.
Relationships between numbers
sns.scatterplot(data=orders, x="distance_km", y="delivery_mins", hue="city", alpha=0.5)
sns.regplot(data=orders, x="distance_km", y="delivery_mins", scatter_kws={"alpha": 0.3})alpha makes overlapping points visible; regplot adds a trend line.
Trends over time
daily = orders.groupby([orders["order_date"].dt.date, "city"])["amount"].sum().reset_index()
sns.lineplot(data=daily, x="order_date", y="amount", hue="city")Heatmaps
pivot = orders.pivot_table(index="city", columns="cuisine", values="rating", aggfunc="mean")
sns.heatmap(pivot, annot=True, fmt=".1f", cmap="YlGnBu")Also useful for a correlation matrix: sns.heatmap(df.corr(numeric_only=True), annot=True, cmap="coolwarm", center=0).
Small multiples with facets
sns.displot(data=orders, x="delivery_mins", col="city", col_wrap=3, height=3)
sns.relplot(data=orders, x="distance_km", y="delivery_mins", col="city", kind="scatter")Facets draw the same chart for each group in a grid, often clearer than many colours in one chart.
Finishing touches with Matplotlib
ax = sns.boxplot(data=orders, x="city", y="delivery_mins")
ax.set_title("Delivery times are longest in the evening-heavy cities")
ax.set_xlabel("")
ax.set_ylabel("Minutes")
plt.tight_layout()Seaborn returns Matplotlib axes, so titles, labels and annotations work as usual.
Which chart?
| Question | Seaborn function |
|---|---|
| Shape of one number | histplot, kdeplot |
| Compare a number across groups | boxplot, violinplot, barplot |
| How many in each category | countplot |
| Two numbers related? | scatterplot, regplot |
| Change over time | lineplot |
| Two categories × a number | heatmap |
| Same chart per group | displot / relplot / catplot with col |
Practise
Our Seaborn track covers distributions, categories, relationships and facets and themes, all running in your browser.
Practise what you just read
Reading helps; writing the code yourself is what sticks. Start here: