您好,我正在尝试向我的 5 个类别和 2 个值(旧的和年轻的)添加百分比countplot。我尝试添加来自
如何在seaborn中的条形顶部添加百分比?
我的代码:
plt.figure(figsize =(7,5))
ax = sb.countplot(data = df_x_1, x = 'concern_virus', hue = 'age')
plt.xticks(size =12)
plt.xlabel('Level of Concern', size = 14)
plt.yticks(size = 12)
plt.ylabel('Number of People', size = 12)
plt.title("Older and Younger People's Concern over the Virus", size = 16)
ax.set_xticklabels(ax.get_xticklabels(), rotation=40, ha="right");
for p in ax.patches:
percentage = '{:.1f}%'.format(100 * p.get_height()/total)
x = p.get_x() + p.get_width()
y = p.get_height()
ax.annotate(percentage, (x, y),ha='center')
plt.show()
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,百分比没有意义。
如何对这个图进行排序以从最大到最小显示?我尝试使用 sort_values 但不起作用
plt.figure(figsize=(15,8))
sns.countplot(x='arrival_date_month',data=df.sort_values('arrival_date_month',ascending=False))
plt.xticks(rotation=45)
Run Code Online (Sandbox Code Playgroud)
我需要显示条形图的百分比。但是我不知道该怎么做。
sns.set_style('whitegrid')
sns.countplot(y='type',data=df,palette='colorblind')
plt.xlabel('Count')
plt.ylabel('Type')
plt.title('Movie Type in Disney+')
plt.show()
Run Code Online (Sandbox Code Playgroud)