Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to limit the number of groups shown in a Seaborn countplot using Matplotlib?
To limit the number of groups shown in a Seaborn countplot, you can use the order parameter to control which categories are displayed. This is particularly useful when dealing with datasets that have many categories and you want to show only the top N most frequent ones.
Steps
Import required libraries (pandas, seaborn, matplotlib)
Create a DataFrame with categorical data
Use
value_counts()to identify the most frequent categoriesApply the
orderparameter incountplot()to limit groupsDisplay the plot with limited categories
Example − Limiting Groups in Countplot
Here's how to create a countplot showing only the top N most frequent categories ?
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# Create sample data with multiple categories
np.random.seed(42)
categories = ['Category_A', 'Category_B', 'Category_C', 'Category_D', 'Category_E', 'Category_F']
data = np.random.choice(categories, size=100, p=[0.3, 0.25, 0.2, 0.15, 0.08, 0.02])
df = pd.DataFrame({'category': data})
# Set the number of groups to display
group_count = 3
# Create figure with subplots
plt.figure(figsize=(12, 5))
# Plot 1: All categories
plt.subplot(1, 2, 1)
sns.countplot(data=df, x='category', color='lightblue')
plt.title('All Categories')
plt.xticks(rotation=45)
# Plot 2: Limited to top N categories
plt.subplot(1, 2, 2)
top_categories = df['category'].value_counts().head(group_count).index
sns.countplot(data=df, x='category', color='coral',
order=top_categories)
plt.title(f'Top {group_count} Categories')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
# Display the counts for verification
print("Category counts:")
print(df['category'].value_counts())
Category counts: category Category_A 30 Category_B 25 Category_C 20 Category_D 15 Category_E 8 Category_F 2 Name: count, dtype: int64
Method − Using Different Approaches
You can also filter the DataFrame before plotting or use different sorting methods ?
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# Create sample data
np.random.seed(42)
products = ['Product_1', 'Product_2', 'Product_3', 'Product_4', 'Product_5', 'Product_6', 'Product_7']
sales_data = np.random.choice(products, size=200, p=[0.25, 0.20, 0.15, 0.15, 0.10, 0.10, 0.05])
df = pd.DataFrame({'product': sales_data})
# Method 1: Using order parameter with top N
group_count = 4
plt.figure(figsize=(10, 6))
plt.subplot(2, 1, 1)
top_products = df['product'].value_counts().head(group_count).index
sns.countplot(data=df, x='product', order=top_products, palette='viridis')
plt.title(f'Top {group_count} Products (Using order parameter)')
# Method 2: Filter DataFrame first
plt.subplot(2, 1, 2)
filtered_df = df[df['product'].isin(top_products)]
sns.countplot(data=filtered_df, x='product', palette='plasma')
plt.title(f'Top {group_count} Products (Using filtered DataFrame)')
plt.tight_layout()
plt.show()
Key Parameters
Understanding the important parameters for limiting countplot groups ?
order− Specifies the order of categories to displayvalue_counts().head(n)− Gets the top N most frequent categoriesvalue_counts().index− Extracts category names from the countsisin()− Filters DataFrame to include only specified categories
Comparison
| Method | Pros | Best For |
|---|---|---|
Using order parameter |
Clean, direct approach | Simple category limiting |
| Filtering DataFrame first | More control over data | Complex filtering logic |
Using value_counts()
|
Automatic frequency sorting | Showing most common categories |
Conclusion
Use the order parameter with value_counts().head(n).index to limit groups in Seaborn countplots. This approach shows only the most frequent categories, making your visualizations cleaner and more focused.
