我想根据色调在条形顶部添加百分比。这意味着所有红色和蓝色条分别等于 100%。
我可以使蓝色条等于 100%,但红色条不能。哪些部分需要修改?
导入和样本数据
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# sample data
np.random.seed(365)
rows = 100000
data = {'Call_ID': np.random.normal(10000, 8000, size=rows).astype(int),
'with_client_nmbr': np.random.choice([False, True], size=rows, p=[.17, .83]),
'Type_of_Caller': np.random.choice(['Agency', 'EE', 'ER'], size=rows, p=[.06, .77, .17])}
all_call = pd.DataFrame(data)
Call_ID with_client_nmbr Type_of_Caller
0 11343 True EE
1 14188 True Agency
2 16539 False EE
3 23630 True ER
4 -7175 True EE
汇总和绘图
df_agg= all_call.groupby(['Type_of_Caller','with_client_nmbr'])['Call_ID'].nunique().reset_index()
ax = sns.barplot(x='Type_of_Caller', y='Call_ID', hue='with_client_nmbr',
data=df_agg,palette=['orangered', 'skyblue'])
hue_order = all_call['with_client_nmbr'].unique()
df_f = sum(all_call.query("with_client_nmbr==False").groupby('Type_of_Caller')['Call_ID'].nunique())
df_t = sum(all_call.query("with_client_nmbr==True").groupby('Type_of_Caller')['Call_ID'].nunique())
for bars in ax.containers:
if bars.get_label() == hue_order[0]:
group_total = df_f
else:
group_total = df_t
for p in ax.patches:
width = p.get_width()
height = p.get_height()
x, y = p.get_xy()
ax.annotate(f'{(height/group_total):.1%}', (x + width/2, y + height*1.02), ha='center')
plt.show()
-
print(hue_order)
is ['False', 'True']