Seaborn Heatmap Currency Format

2019-09-24 00:51发布

问题:

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.))