0

I am trying to add thousands comma separator to the displayed values above each bar in the plot below (example, (currently) 13815 (want to be) 13,815). I am able to do so for the y-axis. Please advise!

My code looks like this:

a = df.opp.value_counts().plot(kind='bar')

#annotate bar plot with values
for p in a.patches:
    a.annotate(str(p.get_height()), (p.get_x() * 1.005, p.get_height() * 1.005))
    a.set_yticklabels(['{:,}'.format(int(x)) for x in bar_uk19_opptype.get_yticks().tolist()])

Plot

Aaron
  • 10,133
  • 1
  • 24
  • 40
mmbbb
  • 29
  • 5
  • Does this answer your question? [How to print number with commas as thousands separators?](https://stackoverflow.com/questions/1823058/how-to-print-number-with-commas-as-thousands-separators) – Aaron Feb 02 '21 at 05:09
  • 1
    Instead of: `str(p.get_height())` use: `'{:n}'.format(p.get_height())` – Aaron Feb 02 '21 at 05:11

1 Answers1

1

Use:

a.annotate("{:,}".format(int(p.get_height())), (p.get_x() * 1.005, p.get_height() * 1.005))

EDIT: Just realized Aaron had a similar ans in the comments alr

MusHusKat
  • 438
  • 2
  • 9
  • The difference here is using `,` vs `n` is that `,` will always use the comma character, and `n` will use the language settings of the computer to choose the correct character – Aaron Feb 02 '21 at 05:59
  • 1
    Thank you so much! Had tried a variation of that but used int() wrongly. Cheers – mmbbb Feb 02 '21 at 06:41