Ravindra BagaleCourses & study guides मराठी Track your progress

14. Visualization with seaborn

Data Science

14.3 hue and boxplot

fig, ax = plt.subplots(figsize=(8, 4))
sns.countplot(data=df, x="city", hue="status", ax=ax)
ax.set_title("City × status counts (fictional)")
plt.xticks(rotation=30, ha="right")
fig.tight_layout()
plt.show()
fig, ax = plt.subplots()
sns.boxplot(data=df, x="festival", y="amount", ax=ax)
ax.set_title("Amount spread by festival (fictional)")
plt.show()

Steps in Jupyter

  1. sns.set_theme(style="whitegrid").
  2. countplot of city.
  3. barplot of mean amount by festival.
  4. countplot with hue="status".
  5. Save one figure to figures/seaborn_city_status.png.

What you should see. Diwali mean amount higher in this tiny sample; Cancelled only under Kolhapur in the hue chart.

Ravindra Bagale's Tip

hue="status" already splits each city bar by status, so you do not need a loop for each category. In sns.boxplot, the line inside each box is the median, not the mean. In sns.barplot, the bar height is the mean by default and the thin black line is a 95% confidence interval; pass estimator="sum" when you want totals.