首页 > 编程语言 >python-绘制对称条形图并添加数据标签

python-绘制对称条形图并添加数据标签

时间:2022-12-21 21:33:12浏览次数:42  
标签:use python 标签 app 0.5 df color ax 条形图

坐标轴显示数据的百分比,小数形式;
数据标签展示绝对值;
添加参考线。

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()

标签:use,python,标签,app,0.5,df,color,ax,条形图
From: https://www.cnblogs.com/wxyz94/p/16997284.html

相关文章