条形图标签显示完整数字而不是科学公式

barplot labels to show full numbers instead of scientific formula

我正在使用 this kaggle dataset 来执行我的数据分析。 我将 'course_students_enrolled' 列值转换为整数。 当我绘制学生人数最多的课程时,我得到一个数字,其中代表 1M 以上值的条形标签显示为 1.3e+06 之类的公式,而不是显示完整数字。图表上方的数字也是如此。 这是我的情节代码:

popularity_filt = (main_df['course_students_enrolled'].sort_values(ascending=False))
most_pop_courses = main_df.iloc[popularity_filt.index][:10]
course_titles = most_pop_courses['title'].to_list()
data = most_pop_courses[['title','course_students_enrolled']]
sns.set_context(font_scale=3, rc={'font.size':12, 'axes.labelsize':20})
sns.set_style('darkgrid')
palette = sns.color_palette(palette="Greens_d", n_colors=len(data))
plt.figure(figsize=(12,6))
ax = sns.barplot(x='title', y='course_students_enrolled', data=data, palette=np.array(palette[::-1]));
ax.set_xticklabels(course_titles, rotation=40, ha='right')
ax.bar_label(ax.containers[0]);

plt.show()

有没有办法显示完整的数字?

您可以使用 f-string:

将其转换为适当的字符串
ax.bar_label(ax.containers[0], labels=[f'{x.get_height():.0f}' for x in ax.containers[0]])