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
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
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
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
countplotwhen you just need frequencies, not an aggregated numeric column. - Pass
order=to control category order, especially for sorted or custom sequences. - Combine with
hueto compare two categorical columns at once.
Check your understanding
When is countplot the right choice?
countplot just counts rows per category with no aggregation of a numeric column.
Show answer
B — You want how many rows fall in each category — countplot just counts rows per category with no aggregation of a numeric column.
How does the lesson order day bars from most to least frequent?
Pass order= with the index of value_counts(), which is already sorted by frequency.
Show answer
D — order=tips["day"].value_counts().index — Pass order= with the index of value_counts(), which is already sorted by frequency.
What does sns.countplot(data=tips, x="day", hue="sex") show?
hue splits each day bar by a second category, so you compare two categorical columns at once.
Show answer
B — Counts per day split by sex — hue splits each day bar by a second category, so you compare two categorical columns at once.
Which Pandas call does countplot effectively replace?
The lesson quotes the McKinney value_counts bar-plot recipe, which countplot does in one call from raw rows.
Show answer
C — s.value_counts().plot.bar() — The lesson quotes the McKinney value_counts bar-plot recipe, which countplot does in one call from raw rows.
Next
Continue to Heatmaps for Correlation to visualize relationships between several numeric columns at once.
🧪 Try It Yourself
Exercise 1 – Count Rows per Category
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Sort by Frequency
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – countplot vs barplot
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.