【发布时间】:2021-05-02 03:30:50
【问题描述】:
这是我的一段代码。 一切都很好,只是显示 0% 是错误的。 这是我的一段代码。怎么了?
import matplotlib.pyplot as plt
ax= pd.crosstab(df4['State'], df4['Status_new']).apply(lambda r: r/r.sum()*100, axis=1)
ax_1 = ax.plot.barh(figsize=(10,10),stacked=True, rot=0)
display(ax)
plt.legend(loc='upper center', bbox_to_anchor=(0.1, 1.0), title="Subject")
plt.xlabel('Status')
plt.ylabel('State')
for rec in ax_1.patches:
height = rec.get_height()
ax_1.text(rec.get_x() + rec.get_width() / 2,
rec.get_y() + height / 2,
"{:.0f}%".format(height),
ha='center',
va='bottom')
plt.show()
这是结果。我需要显示实际百分比。
【问题讨论】:
-
你能发布你的数据框的一部分吗?
-
能否请您纠正正确的语法。当我用“{:.0f}%”.format(rec.get_width()) 替换“{:.0f}%”.format(height) 行时,它不起作用
标签: python pandas matplotlib bar-chart stacked