1

I'm using this kaggle dataset to perform my data analysis. I converted 'course_students_enrolled' column values to integers. When I plot courses with highest numbers of students, I get a figure where bar labels that represent values above 1M appear written in formula like 1.3e+06 instead of showing the full number. Same goes with the number above the chart. This is my code of the plot:

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()

enter image description here

Is there a way to show full numbers instead?

JohanC
  • 71,591
  • 8
  • 33
  • 66
punkuotukas
  • 77
  • 2
  • 10

1 Answers1

1

You can convert it into the appropriate string using f-string:

ax.bar_label(ax.containers[0], labels=[f'{x.get_height():.0f}' for x in ax.containers[0]])
JohanC
  • 71,591
  • 8
  • 33
  • 66
Kosmos
  • 497
  • 3
  • 11
  • There should be some syntax issue in your suggestion. I tried to change quotes and to put 'f' before quotes, but it's not working for me. Could you check if it's working as you wrote it? – punkuotukas Feb 06 '22 at 10:50
  • I’ve changed place so that f is outside the quotes. – Kosmos Feb 06 '22 at 11:01
  • The quotes in your suggestion are not accepted by python. And it's still not working after changing the quotes. This is qhat I get: `TypeError: unsupported format string passed to Rectangle.__format__` – punkuotukas Feb 06 '22 at 11:08
  • I’ve modified the answer. – Kosmos Feb 06 '22 at 11:09
  • once again, python does not accept that type of quotes. `"` or `'` needs to be used. Other than that, your suggestion works, but it does the contrary of what I was trying to achieve. Now all the numbers are shown in scientific formula format. :D Anyway, I changed the `.1E` part to `.0f` and it worked fine (after quotes were changed to the type I indicated above). Anyway, thanks A LOT for your effort to help. If you can modify your answer to make sure that it has all the correct syntax, I'll mark it as answered. – punkuotukas Feb 06 '22 at 11:28
  • Changed the quotation. Wonder if it is the phones keyboard that is screwing it up. – Kosmos Feb 06 '22 at 11:38
  • Could be. Anyway, those single quotes won't work either. Hopefully, this won't be too confusing for a newbie like me. :) – punkuotukas Feb 06 '22 at 11:42
  • 2
    Directly setting the format seems to make more sense: `ax.bar_label(ax.containers[0], fmt='%.0f')` – JohanC Feb 06 '22 at 13:00