首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将条形图文本格式设置为2位小数

将条形图文本格式设置为2位小数
EN

Stack Overflow用户
提问于 2020-02-09 23:29:26
回答 1查看 1.9K关注 0票数 0

我的工作代码如下:

代码语言:javascript
复制
active_parking = pd.pivot_table(parking[parking['Bldg Status'] == 'ACTIVE'],
                                index='Owned/Leased',
                                values='Total Parking Spaces',
                                aggfunc='mean'
                                )
active_parking['% of Total'] = ((active_parking['Total Parking Spaces'] / active_parking['Total Parking Spaces'].sum()) * 100)
print(active_parking, '\n')

active_parking.plot(kind='bar')
for i, number in enumerate(active_parking['Total Parking Spaces']):
    plt.text(x=i, y=number, s=number, horizontalalignment='center', weight='bold')
for i, number in enumerate(active_parking['% of Total']):
    plt.text(x=i, y=number, s=number, horizontalalignment='left', weight='bold')
plt.xticks(rotation=0)
plt.show()

并生成以下输出:

代码语言:javascript
复制
              Total Parking Spaces  % of Total
Owned/Leased                                  
LEASED                   44.707349   37.546059
OWNED                    74.365997   62.453941 

但是我的计划有两个我似乎无法解决的问题:

代码语言:javascript
复制
1)  I want the displayed value on top of each bar limited to two decimal places.
2)  After solving the above issue, how do I center the value over each bar?

即使我用这个: pd.options.display.float_format = '{:.2f}'.format裁剪文本显示,绘图仍然显示14位小数。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-09 23:32:04

使用:

代码语言:javascript
复制
active_parking.plot(kind='bar')
for i, number in enumerate(active_parking['Total Parking Spaces']):
    plt.text(x=i-.23, y=number + 0.9, s=round(number, 2), weight='bold')
for i, number in enumerate(active_parking['% of Total']):
    plt.text(x=i+.02, y=number + 0.9, s=round(number, 2), weight='bold')
plt.xticks(rotation=0)
plt.ylim(top=active_parking.values.max() + 8)
plt.show()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60138199

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档