sns.relplot is the figure-level relational plot. It builds a grid of scatter or line Axes. col and row split the data; kind is "scatter" (default) or "line".
Goal
Facet a scatter by city, then a line grid by product.
Scatter facets
df = pd.DataFrame(
{
"city": ["Nairobi", "Nairobi", "Mombasa", "Mombasa", "Kisumu", "Kisumu"] * 2,
"product": ["A", "B"] * 6,
"units": [12, 7, 9, 4, 11, 3, 10, 8, 6, 5, 14, 2],
"price": [10.5, 22.0, 10.5, 22.0, 10.5, 22.0] * 2,
}
)
df["revenue"] = df["units"] * df["price"]
sns.relplot(data=df, x="units", y="revenue", hue="product", col="city")
plt.show()Each city gets its own column. The hue legend is shared.
Wrap columns
rng = np.random.default_rng(0)
n = 60
df = pd.DataFrame(
{
"city": rng.choice(
["Nairobi", "Mombasa", "Kisumu", "Nakuru", "Eldoret"], size=n
),
"units": rng.integers(1, 20, size=n),
"price": rng.choice([10.5, 22.0], size=n),
}
)
df["revenue"] = df["units"] * df["price"]
sns.relplot(
data=df,
x="units",
y="revenue",
hue="city",
col="city",
col_wrap=3,
height=2.6,
aspect=1.1,
)
plt.show()Line kind
df = pd.DataFrame(
{
"month": ["Jan", "Feb", "Mar", "Apr"] * 3,
"city": ["Nairobi"] * 4 + ["Mombasa"] * 4 + ["Kisumu"] * 4,
"rain": [50, 40, 80, 150, 20, 15, 30, 90, 70, 80, 120, 180],
}
)
df["month"] = pd.Categorical(
df["month"], categories=["Jan", "Feb", "Mar", "Apr"], ordered=True
)
sns.relplot(data=df, x="month", y="rain", hue="city", kind="line", marker="o")
plt.show()relplot returns a FacetGrid, not an Axes. Call plt.show() anyway. Titles live on the grid; plt.title only hits one subplot.
Pitfall
Do not mix plt.subplots() with relplot in the same script. Pick one figure-level call, or use scatterplot on an Axes you already created.