1

I'm trying to print out this stacked bar chart and I want to add a comma after numbers in thousands. For example, if 7000 -> 7,000; if 20000 -> 20,000.

I'm trying to add the code fmt='{:,.0f} to my graph but it is not giving me anything and I can't find a good explanation for this formatting either.

My full code:

ax = dfp_vort_monthly.plot.bar(stacked=True, figsize=(16, 10), ylabel='UNCONVERTED CASE COUNTS', xlabel='WEEK ENDING', title='VOORTMAN Weekly Percentage Bins and Case Counts')

for c in ax.containers:
    label = c.get_label()
    labels = [v.get_height() if v.get_height() > 0 else '' for v in c]
    ax.bar_label(c, labels=labels, label_type='edge',fmt='{:,.0f}', fontweight='bold', color= 'blue')
_ = ax.legend(title='Percentage Bins', bbox_to_anchor=(1.0, 1.0), loc='upper left')
_ = ax.margins(y=0.3)
not_speshal
  • 22,093
  • 2
  • 15
  • 30
vanetoj
  • 103
  • 11

1 Answers1

1

The reason why your code is not working is because you are trying to use the fmt parameter with the bar_label() method, but the bar_label() method does not support the fmt parameter. Instead, you need to use the labels parameter to format the labels. Change your code with

ax = dfp_vort_monthly.plot.bar(stacked=True, figsize=(16, 10), ylabel='UNCONVERTED CASE COUNTS', xlabel='WEEK ENDING', title='V Weekly 
   Percentage Bins and Case Counts')

for c in ax.containers:
   label = c.get_label()
   labels = [f'{v.get_height():,.0f}' if v.get_height() > 0 else '' for v in c]
   ax.bar_label(c, labels=labels, label_type='edge', fontweight='bold', color='blue')
_ = ax.legend(title='Percentage Bins', bbox_to_anchor=(1.0, 1.0), loc='upper left')
_ = ax.margins(y=0.3)

This code will format the labels on your bar chart with a comma separator and no decimal places.

vanetoj
  • 103
  • 11
nischal sharma
  • 479
  • 1
  • 14