Count Plots
When to use count plots
Count plots answer:
- How many records are in each category?
- Is there imbalance?
Examples:
- Orders by status
- Users by plan
- Tickets by priority
flowchart LR A["Raw rows"] --> B["Group by category"] B --> C["Count rows
in each group"] C --> D["Draw one bar
per category"]
Basic countplot
countplot
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
plt.figure(figsize=(6, 4))
sns.countplot(data=tips, x="day")
plt.title("Count by day")
plt.tight_layout()
plt.show()countplot
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
plt.figure(figsize=(6, 4))
sns.countplot(data=tips, x="day")
plt.title("Count by day")
plt.tight_layout()
plt.show()Sorted order
Sorted categories
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
order = tips["day"].value_counts().index
plt.figure(figsize=(6, 4))
sns.countplot(data=tips, x="day", order=order)
plt.title("Count by day (sorted)")
plt.tight_layout()
plt.show()Sorted categories
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
order = tips["day"].value_counts().index
plt.figure(figsize=(6, 4))
sns.countplot(data=tips, x="day", order=order)
plt.title("Count by day (sorted)")
plt.tight_layout()
plt.show()Count with hue
Countplot with hue
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
plt.figure(figsize=(7, 4))
sns.countplot(data=tips, x="day", hue="sex")
plt.title("Count by day split by sex")
plt.tight_layout()
plt.show()Countplot with hue
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
plt.figure(figsize=(7, 4))
sns.countplot(data=tips, x="day", hue="sex")
plt.title("Count by day split by sex")
plt.tight_layout()
plt.show()Visualize it
Each bar’s height is simply “how many rows had this category” — nothing is averaged:
Tips
- Use
countplotcountplotwhen you just need frequencies, not an aggregated numeric column. - Pass
order=order=to control category order, especially for sorted or custom sequences. - Combine with
huehueto compare two categorical columns at once.
Next
Continue to Heatmaps for Correlation to visualize relationships between several numeric columns at once.
🧪 Try It Yourself
Exercise 1 – Count Rows per Category
Exercise 2 – Sort by Frequency
Exercise 3 – countplot vs barplot
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
