Python / Data Science Essentials Interview Questions
How do you compare distributions across categories using Seaborn categorical plots?
Comparing how a numeric variable's distribution differs across groups is one of the most common analytical tasks. Seaborn's categorical plot family gives you progressively more information from left to right: bar (mean only) → box (five-number summary) → violin (full distribution shape) → strip/swarm (individual points).
import seaborn as sns import matplotlib.pyplot as plt tips = sns.load_dataset('tips') fig, axes = plt.subplots(2, 2, figsize=(14, 10)) # Bar plot â mean + 95% CI error bars sns.barplot(data=tips, x='day', y='tip', hue='sex', palette='Set2', ax=axes[0, 0]) axes[0, 0].set_title('Mean Tip by Day and Sex') # Box plot â median, IQR, whiskers, outlier dots sns.boxplot(data=tips, x='day', y='total_bill', hue='smoker', palette='pastel', ax=axes[0, 1]) axes[0, 1].set_title('Total Bill Distribution by Day and Smoker') # Violin plot â box + KDE combined sns.violinplot(data=tips, x='day', y='tip', inner='quartile', # show quartile lines inside palette='muted', ax=axes[1, 0]) axes[1, 0].set_title('Tip Violin by Day') # Strip + box overlay â all points + summary sns.boxplot(data=tips, x='time', y='tip', color='lightblue', ax=axes[1, 1], width=0.4) sns.stripplot(data=tips, x='time', y='tip', color='navy', alpha=0.4, jitter=True, ax=axes[1, 1]) axes[1, 1].set_title('Tip by Time â Box + All Points') plt.tight_layout(); plt.show() # Figure-level catplot for easy faceting sns.catplot(data=tips, x='day', y='tip', hue='sex', col='time', kind='violin', height=5, aspect=0.8)
When to use each: bar plots are fine for comparing means but hide distributional information. Box plots add spread and outliers. Violin plots reveal multi-modality (two bumps indicating two groups within a category). Strip/swarm overlays add individual points, essential for small datasets where a box plot can be misleading with n < 30.
More Related questions...