Skip to content

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

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()

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did