Python Central HubSign in

Data Analytics with Python / Phase 6: Statistical Visualization with Seaborn

Count Plots

Visualize category frequencies using Seaborn countplot and learn ordering and grouping.

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
diagram How countplot works mermaid
countplot tallies rows per category automatically — no separate aggregation step needed.

Basic countplot

Python
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

Python
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

Python
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:

sketch Category counts as bars p5.js
Bar height is a raw tally of rows per category, unlike barplot which averages a numeric column.

Tips

  • Use countplot when you just need frequencies, not an aggregated numeric column.
  • Pass order= to control category order, especially for sorted or custom sequences.
  • Combine with hue to compare two categorical columns at once.

Check your understanding

quizCheck yourself
  1. When is countplot the right choice?

    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.

  2. How does the lesson order day bars from most to least frequent?

    Show answer

    D — order=tips["day"].value_counts().index — Pass order= with the index of value_counts(), which is already sorted by frequency.

  3. What does sns.countplot(data=tips, x="day", hue="sex") show?

    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.

  4. Which Pandas call does countplot effectively replace?

    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

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Sort by Frequency

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – countplot vs barplot

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did