0

I want to transform the below graph made in seaborn to show y axis in percentages.Like 15% 20%... I tried # ylabel=['{:.2}'.format(x)+'%' for x in [2.2,2.4,2.6,2.8,3.0,3.2,3.4,3.6]] but it didn't work. What shall i change?

enter image description here

df_graph2['day']=df['day'].unique().tolist()
df_graph2['Contactable_Percent']=[0]*len(df['day'].unique().tolist())

for pos,value in enumerate(df['day'].unique().tolist()):
    print(pos)
    df_graph2['Contactable_Percent'].iloc[pos]=(df.query('day==@value').query('Completed==1').shape[0])/(df.query('day==@value')['Completed'].shape[0])  

df_graph2=df_graph2.reindex([5,4,3,2,1,0])

sns.set_style("white",{'xtick.color': '.1','xtick.minor.size':16})
plt.figure(figsize=(10, 8))


ax=sns.barplot(x="day", y="Contactable_Percent", data=df_graph2, ci=None,color='b',saturation=1,edgecolor=(0,0,0))
ax.set(ylim=(0.14,.38))
sns.set(font_scale = 2)

ax.set_ylabel('')    
ax.set_xlabel('')
ax.set_xticklabels(ax.get_xticklabels(), rotation=90)


plt.title("Week 35: 24-30 Aug", size=26)

EDIT**********

EDIT 2

  • 2
    Have you tried this https://stackoverflow.com/questions/31357611/format-y-axis-as-percent? – perl Sep 01 '20 at 17:33

1 Answers1

0

When parsing a number (int or float) to a string, you can use the '%' formatting argument as follows:

f'{0.21:%}' => '21.000000%'

Your approach was very close to working out, there was just one tiny mistake. You need to put the '%' into the curly braces of the formated string, not append it to the number:

ylabel = [f'{i:.0%}' for i in df_graph2['Contactable_Percent']]

then set this list as ylabel:

ax.set_yticklabels(ylabel)
David
  • 16
  • 3
  • hi david, thanks for your reply. tried ylabel=[f'{x:.0%}' for x in df_graph2['Contactable_Percent'].sort_values()]. Successful in changing the ylabels to percentages. However the bar values aren't changing dynamically with this label change. How do i change this? –  Sep 02 '20 at 05:30
  • What do you mean by saying "changing dynamically"? Which bar labels are you talking about? the individual bars aren't labeled in your given code – David Sep 12 '20 at 16:05