14. Visualization with seaborn
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
sns.set_theme(style="whitegrid").- countplot of city.
- barplot of mean amount by festival.
- countplot with
hue="status". - 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.
Ravindra Bagale's Tip – मराठी
hue="status" प्रत्येक city चा bar status नुसार आधीच वेगळा करतो, म्हणून categories साठी वेगळा loop लिहिण्याची गरज नाही. sns.boxplot मध्ये box च्या आतली रेषा median असते, mean नाही. sns.barplot मध्ये bar ची उंची default ने mean असते आणि वरची बारीक काळी रेषा 95% confidence interval असते; total हवा असेल तर estimator="sum" द्या.
Ravindra Bagale's Tip – हिंदी
hue="status" हर city के bar को status के हिसाब से पहले ही बाँट देता है, इसलिए हर category के लिए loop लिखने की ज़रूरत नहीं है। sns.boxplot में box के अंदर की रेखा median होती है, mean नहीं। sns.barplot में bar की ऊँचाई default में mean होती है और ऊपर की पतली काली रेखा 95% confidence interval होती है; total चाहिए तो estimator="sum" दो।