簡體   English   中英

使用 Seaborn 反轉色調類別的順序和顏色

[英]Invert order and color of hue categories using Seaborn

因此,當我繪制箱形圖並在“色調”中使用 cotagory 時,我會得到類別的默認順序(在我的情況下是 Yes 和 No)。 我希望這些框以相反的順序(否是)和相反的 colors 呈現。 這是我的代碼:

fig=plt.figure(figsize=(12,6))
df2=df[df["AMT_INCOME_TOTAL"]<=df["AMT_INCOME_TOTAL"].median()]
ax = sns.boxplot(x="NAME_FAMILY_STATUS", y="AMT_INCOME_TOTAL", data=df2,hue="FLAG_OWN_REALTY")
plt.legend(bbox_to_anchor=(1.05,1),loc=2, borderaxespad=1,title="Own Property flag")
plt.title('Income Totals per Family Status for Bottom Half of Earners')

在此處輸入圖像描述

我想首先擁有“N”類別並使用藍色。

如上所述,如果您想明確指定框的順序和hue_order,這可以完成工作:

fig=plt.figure(figsize=(12,6))
df2=df[df["AMT_INCOME_TOTAL"]<=df["AMT_INCOME_TOTAL"].median()]
ax = sns.boxplot(x="NAME_FAMILY_STATUS", y="AMT_INCOME_TOTAL", data=df2,hue="FLAG_OWN_REALTY",order=['Married',"Civil marriage","Widow","Single / not married","Separated"], hue_order = ['N', 'Y'])
plt.legend(bbox_to_anchor=(1.05,1),loc=2, borderaxespad=1,title="Own Property flag")
plt.title('Income Totals per Family Status for Bottom Half of Earners')

只需在 sns.boxplot() 上添加 hue_order

ax = sns.boxplot(x="NAME_FAMILY_STATUS", y="AMT_INCOME_TOTAL", data=df2,hue="FLAG_OWN_REALTY", hue_order = ['N', 'Y'])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM