我的工作代码如下:
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()并生成以下输出:
Total Parking Spaces % of Total
Owned/Leased
LEASED 44.707349 37.546059
OWNED 74.365997 62.453941 但是我的计划有两个我似乎无法解决的问题:
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位小数。
发布于 2020-02-09 23:32:04
使用:
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()https://stackoverflow.com/questions/60138199
复制相似问题