sns.catplot is the figure-level categorical plot. kind picks the axes function: "bar", "count", "box", "violin", "strip", "swarm", "point". col and row facet the grid.
Goal
Facet a bar chart by product, then a box grid by city.
Bar 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.catplot(
data=df,
x="city",
y="revenue",
hue="product",
kind="bar",
col="product",
errorbar=None,
)
plt.show()Box kind
rng = np.random.default_rng(0)
rows = []
for city in ["Nairobi", "Mombasa", "Kisumu"]:
for product, loc in [("A", 10), ("B", 14)]:
rows.append(
pd.DataFrame(
{
"city": city,
"product": product,
"units": rng.normal(loc, 2.5, size=20),
}
)
)
df = pd.concat(rows, ignore_index=True)
sns.catplot(data=df, x="product", y="units", col="city", kind="box")
plt.show()Strip kind
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.catplot(data=df, x="city", y="revenue", hue="product", kind="strip", jitter=True)
plt.show()Same rule as relplot: call plt.show(), and do not mix with plt.subplots().
You should see
Relational grids (scatter and line) use Relplot. Categorical grids use catplot.