Skip to main content

Seaborn (Part 3): Relational and categorical plots

With distributions covered in Part 2, relational plots answer joint behavior (“as X climbs, does Y drift?”), while categorical encodings highlight strata.


📚 Prerequisites

  • Tidy DataFrames (one observation per row).

🎯 What you'll master

  • Encode third dimensions with hue, style, size.
  • Prefer catplot when faceting categorical comparisons.

Relational grids

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd

tips = sns.load_dataset("tips") # bundled sample data

sns.relplot(
data=tips,
x="total_bill",
y="tip",
hue="time",
col="sex",
kind="scatter",
height=3.8,
)
plt.show()

relplot returns a facet grid—it is ideal when you iterate categories without manual subplot loops.


Categorical bar estimates

sns.catplot(
data=tips,
x="day",
y="total_bill",
hue="smoker",
kind="bar",
errorbar=("ci", 95),
height=4,
)
plt.show()

Bars summarize central tendency (default mean) plus confidence intervals—state both when publishing.


💡 Key takeaways

  • relplot / catplot trade fine-grained Matplotlib tweaking for succinct faceting clauses.
  • Never stack unrelated metrics on identical axes scales without normalization—readers confuse units.

➡️ Next steps

Step into dashboards with Plotly and Dash (introduction).