While I am trying to produce a heatmap compare the performance of different user segments across time periods, I cannot customize the annotation of the heatmaps in EngFormatter style (i.e. 1233 = 1k, 10000 = 10k, 2000000 = 2M, etc.). I could not find where is the location of the annotation text element under each heatmap within the FacetGrid.
How to customize the unit of Facegrid Annotation?
My effort:
import matplotlib.pyplot as plt
import seaborn as sns
def draw_heatmap(*args, **kwargs):
data = kwargs.pop('data')
d = data.pivot(index=args[1], columns=args[0], values=args[2])
sns.heatmap(d, **kwargs)
fg = sns.FacetGrid(df, col="time_interval",height=6)
fg.map_dataframe(
draw_heatmap,
'user_action', 'segment', 'users',
cbar=True, cmap="Blues", square = True, annot=True,fmt=".2g"
)
from matplotlib.ticker import EngFormatter
for t in fg.texts: t.set_text(EngFormatter(t.get_text()))
Current output:
Desired output:
The displayed annotations of heatmaps are in EngFormatter style
Sample dataset:
index,time_interval,user_action,segment,users
0,time_2,,Super Heavy,1233
1,time_1,Click Use,Medium,10000
2,time_1,Click View Detail,Light,2000000
3,time_2,Click View Detail,Medium,3999
4,time_1,Click See All,Medium,14542
Session info:
polars==0.17.3
pandas==1.5.3
seaborn==0.12.2