python-绘制对称条形图并添加数据标签
坐标轴显示数据的百分比,小数形式;
数据标签展示绝对值;
添加参考线。
app_use_df = pd.read_excel('/Users/wang/Desktop/data.xlsx'
# ,index_col=0
,keep_default_na=0)
app_use_df['not_use_rate'] = 1- app_use_df['use_rate']
fig = plt.figure(figsize=(16,8))
ax = fig.add_subplot(1,1,1)
p1 = ax.barh(app_use_df['district'], app_use_df['use_rate'], label='已使用'
, color='#F08080', height=0.5)
p2 = ax.barh(app_use_df['district'], -app_use_df['not_use_rate'], label='未使用'
, color='#FFA07A', height=0.5)
# # 给条形图添加数据标注
for i, j in enumerate(app_use_df['district']):
ax.text(app_use_df.iloc[:,4][i]+0.02, j, app_use_df.iloc[:,2][i]
, verticalalignment='center', horizontalalignment='center')
for i, j in enumerate(app_use_df['district']):
ax.text(-app_use_df.iloc[:,5][i]-0.02, j, app_use_df.iloc[:,3][i]
, verticalalignment='center', horizontalalignment='center')
ax.axvline(0.5, ls='--', color='r')
ax.axvline(-0.5, ls='--', color='r')
ax.set_title("各区XXXX情况")
ax.legend(loc=2)
plt.show()