首页 > 解决方案 > 在pyplot中给出hue时如何更改图例的命名?

问题描述

数据集:

index TARGET NAME_INCOME_TYPE   count
0   0   Working                 46924
1   0   Commercial associate    21492
2   0   Pensioner               16879
3   0   State servant           6593
4   0   Student                  9
5   0   Unemployed              6
6   0   Businessman              4
7   0   Maternity leave           1
8   1   Working                 4978
9   1   Commercial associate    1726
10  1   Pensioner               978
11  1   State servant           407
12  1   Unemployed              4

图表代码:

plt.figure(figsize=(20,6))
sns.barplot(x='NAME_INCOME_TYPE',y='percent_rate',hue='TARGET',data=qq,palette='winter',saturation=0.5, hue_order=[0,1])
plt.yticks(size=20)
plt.ylabel(size=20, s = '% rate')
plt.xlabel(size=20, s= 'NAME INCOME TYPE')
plt.xticks(rotation=90,size=20)
plt.legend(loc='upper right')
plt.show()

我已经指定了色调并且它工作正常,但是图例将它作为“0, 1”我想将其更改为“是的,否”。

更改 hue_order 参数不起作用。

更改图例不起作用。

标签: seaborn

解决方案


好的,显然色调顺序是正确的参数。我只是没有将“0,1”交换为“1,0”。

我编码:

plt.figure(figsize=(20,6))
sns.barplot(x='NAME_INCOME_TYPE',y='percent_rate',hue='TARGET',data=qq,palette='inferno',saturation=0.5,hue_order=['YES', 'NO'])
plt.yticks(size=20)
plt.ylabel(size=20, s = '% rate')
plt.xlabel(size=20, s= 'NAME INCOME TYPE')
plt.xticks(rotation=90,size=20)
plt.title("Income sources of Applicants in terms of loan is repayed or not  in %",size=20)
plt.legend(loc='upper right')
plt.show()

推荐阅读