Python Seaborn - Catplot
0 1063
Introduction to Seaborn Catplot
Seaborn’s catplot() is a figure-level function designed for visualizing relationships between categorical and numerical data. It wraps around Matplotlib and pandas, offering a high-level yet flexible interface to produce clean, informative plots.
Loading Data and Basic Plot
Start by importing a dataset with categorical variables. For example, the “tips” dataset from Seaborn:
import seaborn as sns
tips = sns.load_dataset("tips")
sns.catplot(x="day", y="total_bill", data=tips)
This creates a default strip plot, showing individual points jittered along the categorical axis.
Changing Plot Types with kind
The kind argument lets you switch between different categorical visualizations:
sns.catplot(x="day", y="total_bill", kind="box", data=tips)
sns.catplot(x="day", y="total_bill", kind="violin", data=tips)
sns.catplot(x="day", y="total_bill", kind="bar", data=tips)
sns.catplot(x="day", kind="count", data=tips)
This covers box, violin, bar, and count plots—each highlighting data differently.
Adding Hue and Facets
You can introduce additional categorical dimensions with hue, col, and row. This splits the data into facets and colors by category:
sns.catplot(x="day", y="total_bill", hue="smoker", kind="bar", data=tips)
sns.catplot(x="time", y="pulse", hue="kind", col="diet",
kind="strip", data=sns.load_dataset("exercise"))
Adjusting Size & Aspect
To better fit multiple facets, define the plot’s size and shape:
sns.catplot(x="day", y="total_bill", kind="violin",
height=4, aspect=1.2, data=tips)
Fine-Tuning with FacetGrid Methods
catplot() returns a FacetGrid object. You can refine labels, titles, and axes using its methods:
g = sns.catplot(x="time", y="pulse", hue="kind",
col="diet", kind="box", data=tips)
g.set_axis_labels("Time of Day", "Pulse Rate")
g.set_titles("{col_name} Diet")
g.despine(left=True)
Customization Tips
- Use
palette(e.g. “Set2”, “viridis”) to modify colors. - Set
orient='h'for horizontal layouts. - Control jitter, split violins, or add saturation for styling details.
Putting It All Together
Here’s a complete example combining facets, styling, and plot type:
sns.set_style("whitegrid")
g = sns.catplot(
x="day", y="total_bill", hue="smoker",
col="time", kind="violin",
height=3, aspect=1.1,
palette="Set2",
data=tips
)
g.set_titles("{col_name}")
g.set_axis_labels("Day", "Total Bill ($)")
g.despine(left=True)
Conclusion
Seaborn’s catplot() is a versatile and user-friendly tool for exploring categorical relationships. Whether you need box plots, count plots, or violin plots—combined with grouping via hue or faceting—you can easily produce polished, insightful visualizations with minimal code.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!
Share:


Comments
Waiting for your comments