The code below is the code I am using for a heart failure analysis project. But,
- This method is not centering the values of each bar under the graph, pictured below.
- I am not getting the percentage value above each bar in the graph
def plot_percentage(df, col, target):
x,y = col, target
temp_df = df.groupby(x)[y].value_counts(normalize=True)
temp_df = temp_df.mul(100).rename('percent').reset_index()
temp_df = temp_df[temp_df.HeartDisease != 0]
order_list = list(df[col].unique())
order_list.sort()
sns.set(font_scale=1.5)
g = sns.catplot(x=x, y='percent', hue=x,kind='bar', data=temp_df, height=8, aspect=2, order=order_list, legend_out=False)
g.ax.set_ylim(0,100)
plt.title(f'{col.title()} By Percent {target.title()}',
fontdict={'fontsize': 30})
plt.xlabel(f'{col.title()}', fontdict={'fontsize': 20})
plt.ylabel(f'{target.title()} Percentage', fontdict={'fontsize': 20})
return g