Seaborn Heatmap Currency Format

倖福魔咒の 提交于 2019-12-13 10:52:18

问题


Given the following heatmap:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
%matplotlib inline
df = pd.DataFrame(
      {'A' : ['A', 'A', 'B', 'B','C', 'C', 'D', 'D'],
       'B' : ['A', 'B', 'A', 'B','A', 'B', 'A', 'B'],
       'C' : [22000, 4000, 500, 20000, 0, 3000, 90000, 1000],
       'D' : [6000, 62000, 7000, 700, 30000, 30, 1000, 1000]})

df=df.pivot('A','B','C')
fig, ax = plt.subplots(1, 1, figsize =(4,6))

sns.heatmap(df, annot=True, linewidths=0, cbar=False)
plt.show()

I would like the values to display as currency in thousands like this: $22K

Bonus question: Display as thousands with one decimal like this: $8.9K

Thanks in advance!


回答1:


df["C"] = df["C"].map(lambda x: "${:,.1f}".format(x/1000.))



来源:https://stackoverflow.com/questions/35003011/seaborn-heatmap-currency-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!