Last update: I figured it out by looking at a million different stackoverflow posts.
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}'))
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]
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
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:
- I'm still not getting value labels and getting an
AttributError: 'AxesSupplot' object has no attribute 'bar_label'
- It is not plotting
Paid
forFiscal Year = 2022
- Y-axis is using scientific notation and I want the
ax.set_ylim(0,10000000)
See screenshot of my new code below
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')
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)
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')
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!!