How to Make Countplot or barplot with Seaborn Catplot?
0 642
How to Make a Countplot or Barplot with Seaborn Catplot
When working with categorical data in Python, visualizing distribution is essential. Seaborn’s catplot() (with kind='count') automatically tallies category frequencies and plots them as bars, saving you from manual aggregation.
Creating a Basic Countplot
Let's use the classic Titanic dataset included in Seaborn:
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_style('darkgrid')
titanic = sns.load_dataset('titanic')
sns.catplot(x='sex', kind='count', data=titanic)
plt.xlabel('Gender')
plt.ylabel('Count')
plt.show()
This produces a clear bar chart that displays the total number of male and female passengers.
Adding a Second Category: Grouped Bars
You can split bars by another variable using hue. Here’s the survival status grouped by gender:
sns.catplot(x='sex', hue='survived', kind='count', data=titanic)
plt.xlabel('Gender')
plt.ylabel('Count')
plt.show()
This shows separate bars for survivors and non-survivors within each gender category—great for comparison.
Flipping Orientation: Horizontal Bars
Using a horizontal layout can improve readability when category names are long or numerous:
sns.catplot(y='sex', hue='survived', kind='count', data=titanic)
plt.xlabel('Count')
plt.ylabel('Gender')
plt.show()
Customizing Plot Dimensions
You can adjust the size and shape of your figure using height and aspect arguments:
sns.catplot(x='sex', hue='survived', kind='count',
data=titanic, height=4, aspect=1.5)
plt.xlabel('Gender')
plt.ylabel('Count')
plt.show()
Example with a Custom Dataframe
For cases where your data isn't pre-counted, Seaborn handles the grouping internally. Here's an example using a fictional developer survey:
import pandas as pd
df = pd.DataFrame({
'Education': ['Bachelor', 'Master', 'PhD', 'Bachelor', 'Master'],
'Gender': ['M', 'F', 'M', 'F', 'F']
})
sns.catplot(x='Education', kind='count', data=df, height=5, aspect=1.2)
plt.xlabel('Education Level')
plt.ylabel('Count')
plt.show()
# Grouped by gender
sns.catplot(x='Education', hue='Gender', kind='count', data=df,
height=5, aspect=1.2)
plt.xlabel('Education Level')
plt.ylabel('Count')
plt.show()
This effectively counts entries per category and optionally per subgroup.
Tips for Effective Counts & Barplots
- Use
kind='count'so Seaborn computes frequencies for you. - Apply
hueto introduce a secondary categorical separation. - Choose between vertical and horizontal layouts using
xory. - Tweak figure dimensions with
heightandaspectfor better visuals. - Remember to label axes clearly using Matplotlib’s
plt.xlabel()andplt.ylabel().
Conclusion
Seaborn’s catplot(kind='count') is a fast and intuitive way to visualize category frequencies. Between grouping with hue, switching orientation, and adjusting figure size, you can create polished, informative bar charts with minimal effort.
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