Categorical plots help visualize data that falls into distinct groups (e.g., countries, companies, education levels). Unlike continuous data, categorical data has no “in-between” values.
📥 Imports
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns📂 The Data
Load a dataset (e.g., dm_office_sales.csv) for analysis:
df = pd.read_csv("dm_office_sales.csv")
df.head()📈 Countplot (Total Count per Category)
A countplot displays the number of occurrences of each category.
plt.figure(figsize=(10,4), dpi=200)
sns.countplot(x='division', data=df)Count of education levels:
plt.figure(figsize=(10,4), dpi=200)
sns.countplot(x='level of education', data=df)🏷️ Breakdown within Another Category (hue)
plt.figure(figsize=(10,4), dpi=200)
sns.countplot(x='level of education', data=df, hue='training level')📌 Customize Colors with Matplotlib Colormaps:
List of colormaps
sns.countplot(x='level of education', data=df, hue='training level', palette='Set1')
sns.countplot(x='level of education', data=df, hue='training level', palette='Paired') # Good for distinct jumps📊 Barplot (Statistical Estimation)
A barplot aggregates a numerical feature (e.g., salary) by a categorical variable. By default, it displays the mean.
plt.figure(figsize=(10,6), dpi=200)
sns.barplot(x='level of education', y='salary', data=df, estimator=np.mean, ci='sd') # Mean with standard deviation🏢 Grouped Barplot with hue
plt.figure(figsize=(12,6))
sns.barplot(x='level of education', y='salary', data=df, estimator=np.mean, ci='sd', hue='division')📍 Moving Legend Outside the Plot
plt.figure(figsize=(12,6), dpi=100)
sns.barplot(x='level of education', y='salary', data=df, estimator=np.mean, ci='sd', hue='division')
plt.legend(bbox_to_anchor=(1.05, 1)) # Moves legend outside