【问题标题】:Python Seaborn Heatmap formatting annotationPython Seaborn Heatmap 格式化注释
【发布时间】:2020-08-26 13:43:43
【问题描述】:

大家好,我正在尝试使用我自己的一些数据重新创建这个 Seaborn Heatmap https://seaborn.pydata.org/examples/heatmap_annotation.html,如下所示:

我很难理解格式化文档并希望得到注释以显示例如437521$438k。这有可能吗?非常感谢!

【问题讨论】:

  • 谢谢@BallpointBen。我更多地指的是情节内的实际数据标签。但也会研究颜色栏格式
  • 谢谢@BallpointBen。我现在已经设法在最后使用for t in ax.texts: t.set_text("$" + t.get_text() + "k") 得到k 和$,但仍然试图弄清楚如何将438521 舍入到438

标签: python pandas seaborn


【解决方案1】:

使用带有添加单位和圆形的自定义函数:

#https://stackoverflow.com/a/45478574/2901002
def human_format_round(number):
    units = ['', 'K', 'M', 'G', 'T', 'P']
    k = 1000.0
    magnitude = int(floor(log(number, k)))
    print (magnitude)
    return '{:.0f}{}'.format(round(number / k**magnitude), units[magnitude])

 for t in ax.texts: 
     t.set_text("$" + human_format_round(int(t.get_text())))

【讨论】:

  • 这很棒。非常感谢@jezrael
猜你喜欢
  • 2019-08-02
  • 1970-01-01
  • 1970-01-01
  • 2020-02-26
  • 2018-08-16
  • 2020-11-28
  • 2017-01-02
  • 1970-01-01
  • 2012-05-05
相关资源
最近更新 更多