繁体   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