python - 如何将 Pandas 绘图注释更改为整数?

标签 python pandas matplotlib

我一直在使用 this 中的代码行发布以便在条形图中的列顶部注释总值,但我似乎无法弄清楚如何获得没有小数位的整数结果?

ax = df.plot.bar(title="Scores")
for p in ax.patches:
    ax.annotate("%.2f" % p.get_height(), (p.get_x() + p.get_width() / 2., p.get_height()), ha='center', va='center', xytext=(0, 10), textcoords='offset points')

有什么想法吗?

最佳答案

只需将数字转换为int:

ax = df.plot.bar(title="Scores")
for p in ax.patches:
    ax.annotate(str(int(p.get_height())), (p.get_x() + p.get_width() / 2., p.get_height()), ha='center', va='center', xytext=(0, 10), textcoords='offset points')

演示:

关于python - 如何将 Pandas 绘图注释更改为整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44893942/

相关文章:

python - 选择要写入的 .txt 文件中的行数

python - 关于索引重复条目的 Pandas 枢轴警告

python - 比较 Python Pandas 中的 2 个不同数据帧

python - 如何在 matplotlib 中定位单个箱线图?

python - 以下三种方式在构造函数中声明属性有什么区别?

python - 仅检测正则表达式中的数字

python - 如何安装基于Django的项目?

python - 如何以某种方式转置 Pandas 数据框?

python - 将 imshow 与共享 x 轴与 matplotlib 一起使用

python - 当鼠标悬停在某个区域(矩形)上时,显示标签/注释