I have a data frame that displays everything I need, but initially my 'No Show (%)' rate calculation displayed in decimal places:
Groups and Classes Booked Arrived No Show (n) No Show (%)
6 Supervised Exercise Program 121 57 64 5e-01
0 Active Living 101 4 2 2 5e-01
2 Eating Well the Mediterranean Way 38 24 14 4e-01
5 MBCT 101 66 35 3e-01
4 Healthy Meal Planning 33 22 11 3e-01
3 Grief 56 46 10 2e-01
1 Craving Change 115 95 20 2e-01
Total NaN 468 312 156 3e-01
This then gives me the following chart:
mergedgcfig = mergedgc.sort_values('No Show (%)', ascending=True)
fig2 = px.bar(mergedgcfig, x='No Show (%)', y='Groups and Classes', text='No Show (%)', title='<b>Groups and classes no show rate</b>', template='simple_white', width=600, height=400)
fig2.update_traces(marker_color='#3EB595')
fig2.update_layout(uniformtext_minsize=8, uniformtext_mode='hide', autosize=False, title_x=0.7, title_font_family ="Calibri")
fig2.write_image('fig2.png')
Which is fine and all but I'd like to display my data labels in percentages instead.
mergedgc['No Show (%)'] = mergedgc['No Show (%)'].transform(lambda x: '{:,.0%}'.format(x))
Groups and Classes Booked Arrived No Show (n) No Show (%)
6 Supervised Exercise Program 121 57 64 53%
0 Active Living 101 4 2 2 50%
2 Eating Well the Mediterranean Way 38 24 14 37%
5 MBCT 101 66 35 35%
4 Healthy Meal Planning 33 22 11 33%
3 Grief 56 46 10 18%
1 Craving Change 115 95 20 17%
Total NaN 468 312 156 33%
So the data frame ends up displaying what I want but the problem is when I try to put this into a bar chart, it reads the percentages as a categorical variable.
What am I doing wrong? I basically just want my No Show calculation to be expressed as percentages and display those percentages in the bar chart data labels. It looks like as soon as you add a % symbol, it converts to a string. I thought recognizing percentages would have been built in so I'm not sure what I'm doing wrong. Can someone help me so I can get the first figure to display, but with percentage data labels?
You just need to change the text
field to text=[f'{i}%' for i in mergedgcfig['No Show (%)']]
.
mergedgcfig = mergedgc.sort_values('No Show (%)', ascending=True)
fig2 = px.bar(mergedgcfig, x='No Show (%)', y='Groups and Classes', title='<b>Groups and classes no show rate</b>',
template='simple_white', width=600, height=400, text=[f'{i}%' for i in mergedgcfig['No Show (%)']])
fig2.update_traces(marker_color='#3EB595')
fig2.update_layout(uniformtext_minsize=8, uniformtext_mode='hide', autosize=False, title_x=0.7, title_font_family ="Calibri")