pythonpandasseabornstring-formattingplot-annotations

How to format heatmap annotation floats to only show the decimal component


I have floats and I'm interested in just the decimal places of them because the integer part is not important (and always will be the same in my problem).

I have to use string formatting because the number will be displayed on seaborn plots. I'm using '0.2f' to display just two decimal, however I don't know how to remove the integer part.

Example:

I want to show .25 instead of 0.25

Details:

The implementation must work to format the values in seaborn.heatmap:

sns.heatmap(table, annot = True, fmt = '.2f') 

but I want to display just the decimals as described above.


Solution

  • import seaborn as sns
    import numpy as np  # for random data
    
    # sample data
    np.random.seed(20231118)
    df = pd.DataFrame(np.random.random(size=(6, 6)))
    
    # create a new f-string formatted dataframe; these will be str dtype
    annot = df.apply(lambda col: [f'{v:.2f}'[-3:] for v in col], axis=0)
    
    # plot with annotations
    ax = sns.heatmap(data=df, fmt='', annot=annot, cbar=False)
    

    enter image description here

    df

              0         1         2         3         4         5
    0  0.321406  0.647402  0.173481  0.885630  0.206138  0.584212
    1  0.617528  0.782675  0.071267  0.858961  0.689791  0.093260
    2  0.161312  0.069866  0.047737  0.804141  0.523107  0.635975
    3  0.508007  0.890942  0.435791  0.281811  0.560090  0.384583
    4  0.264682  0.501962  0.492275  0.143144  0.629373  0.445461
    5  0.883968  0.207125  0.946935  0.222533  0.001788  0.622386
    

    annot

         0    1    2    3    4    5
    0  .32  .65  .17  .89  .21  .58
    1  .62  .78  .07  .86  .69  .09
    2  .16  .07  .05  .80  .52  .64
    3  .51  .89  .44  .28  .56  .38
    4  .26  .50  .49  .14  .63  .45
    5  .88  .21  .95  .22  .00  .62