'Add value labels to stacked bar chart

Last update: I figured it out by looking at a million different stackoverflow posts.

  1. To turn off the scientific notation for tick labels, I added:
    ax.ticklabel_format(useOffset=False,style='plain')
    ax.get_yaxis().set_major_formatter(matplotlib.ticket.FuncFormatter(lambda y, pos: f'{y:,.0f}'))

  2. To turn off the scientific notation for bar_label, I added:
    for container in ax.containers:
    ax.bar_label(container,label_type="center",labels=[f'{x:,.0f}' for x in container.datavalues]

  3. To plot the bars as stacked instead of overlapping, I added:
    p2=ax.bar(pivot.index,pivot['Unrecovered'], bottom=pivot['Paid'], label="Unrecovered")

2nd Update: I upgraded matplotlib to version 3.5.1. As a result, the value labels are showing up but they are still in scientific notation. See screenshot below
enter image description here
Update: Based on user's comments below, I read through the bar_label demo from matplotlib, which was super helpful, and re-wrote my python code to mimic the bar_label demo.

However, I've run into the following problems with my new code:

  1. I'm still not getting value labels and getting an AttributError: 'AxesSupplot' object has no attribute 'bar_label'
  2. It is not plotting Paidfor Fiscal Year = 2022
  3. Y-axis is using scientific notation and I want the ax.set_ylim(0,10000000)

See screenshot of my new code belowenter image description here

Can someone help me debug these issues? Thank you!

Original Post question:

I have a pivot table called claims where the index = 'fiscal years' and the values = {'Claim', 'Paid'}.

pivot = claims.pivot_table(values={'Claim','Paid'}, index='Fiscal Year', aggfunc='sum') claim.pivot_table

I then added calculated values to the pivot table to show 'Unrecovered' and 'Recovered' where 'Recovered' is the percentage of 'Paid' divided by 'Claim'

pivot['Unrecovered'] = pivot['Claim] - pivot['Paid']
pivot['Recovered'] = ((pivot['Paid] / pivot['Claim']) * 100)
Calculate recovered vs. unrecovered

Using the pivot table, I created a stacked bar chart where x = 'Fiscal year' and y = {'Paid' 'Unrecovered'} and with secondary y-axis showing % Recovered

ax = pivot.plot(y = ['Paid','Unrecovered'],kind='bar',rot=0,stacked = True,ylabel = "Amount ($)")

Now I format the y-axis tick labels with commas
ax.get_yaxis().set_major_formatter(matplotlib.ticker.FuncFormatter(lambda y, pos: f'{y:,.0f}'))

And then I add line over stacked bar chart where y ='Unrecovered'

ax1 = pivot.reset_index()["Recovered"].plot(secondary_y = True,color = 'k',marker = 'o')
ax1.set_ylabel("Recovered (%)") # adds a label to the second y-axis
ax1.set_ylim(0,100) # forces the y-axis to show 0 thru 100 instead of decimals

This section below is where I have been trying to add value labels to the stacked bar chart using 'plt.annotate' but nothing actually appears in the chart

for x,y in zip():label = '{:.0f}'.format(y)
plt.annotate(label,(x,y), textcoords="offset points",xytext=(0,10),ha='center')

plt.show()
plot stacked bar chart

I want to add value labels to the stacked bar chart showing the amount for each fiscal year in the center of the bars.

I have tried so many solutions but none seem to work for me. My latest attempt uses plt.annotate but nothing appears in the bar chart.

I will really appreciate if someone could help guide me through this!!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source