Plotting different types of plots using Factor plot in seaborn
0 690
Plotting Different Types of Plots using FactorPlot in Seaborn
Seaborn’s factorplot() (renamed to catplot() in newer versions) offers a powerful way to visualize categorical variables through multiple plot types at once. Whether you want bar charts, box plots, violin plots, or swarm plots, factorplot() helps you explore complex datasets in a clean and faceted manner.
Understanding FactorPlot / CatPlot
The factorplot()/catplot() function combines Seaborn’s FacetGrid with several kinds of categorical plots. This allows you to:
- Use
rowandcolto facet data by categories - Use
hueto style subcategories - Switch between plot types with the
kindparameter (bar, box, violin, strip, swarm, point, count)
Creating a Basic Bar Plot
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
sns.factorplot(x="day", y="total_bill", kind="bar", data=tips)
plt.title("Average Total Bill by Day")
plt.show()
# Updated syntax using catplot
sns.catplot(x="day", y="total_bill", kind="bar", data=tips)
plt.title("Average Total Bill by Day")
plt.show()
This displays the mean total bill for each day of the week. Use the newer catplot() syntax for better compatibility.
Facetting with Hue, Row, and Column
To add subcategory distinctions, use hue. For multi-dimensional plots, combine row and col:
s = sns.catplot(x="day", y="total_bill", hue="sex", kind="box", data=tips, palette="Set2")
s.fig.suptitle("Total Bill by Day and Gender")
s.fig.tight_layout()
s.fig.subplots_adjust(top=0.9)
sns.catplot(x="day", y="total_bill", hue="sex", col="time", kind="violin", data=tips, palette="Set3")
plt.subplots_adjust(top=0.85)
plt.suptitle("Total Bill by Day, Gender, and Time of Day")
plt.show()
You can now compare distributions across days, split by gender, and facet by meal time in one plot.
Using Different Plot ‘Kinds’
Just change the kind parameter to explore different visualizations:
- count: shows the frequency of each category
- strip: plots individual points
- swarm: avoids point overlap
- violin: displays kernel density plots
- point: connects means with point markers
sns.catplot(x="day", kind="count", data=tips)
plt.title("Count of Records by Day")
plt.show()
Customizing Appearance
You can adjust aesthetics using additional parameters:
palette: chooses harmonious color schemesheightandaspect: control figure dimensionssharey/sharex: toggle axis alignment across facets
sns.catplot(x="day", y="total_bill", hue="sex", kind="box",
data=tips, palette="coolwarm", height=6, aspect=1.2, sharey=False)
plt.title("Styled Box Plot with Facets")
plt.show()
When to Use FactorPlot
factorplot()/catplot() is ideal when:
- You’re exploring relationships across multiple categorical variables.
- You want consistent axis scales across subplots.
- You need a clean, faceted presentation for reports or dashboards.
Conclusion
Seaborn’s factorplot()/catplot() is a versatile tool for visual exploration of categorical data. By combining facets, hues, and various plot types, it simplifies multi-dimensional analysis. Use it to effortlessly create comparative visualizations that are both informative and polished.
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