Search code examples
pythondata-visualizationseaborn

Exclude a column from Seaborn Heatmap formatting, but keep in the map


So my table looks kinda like this:

Cl1  Cl2  Cl3  Sum
0.7  0.9  0.9  2.5
0.8  1.5  0.9  3.2
2.4  2.8  2.1  7.3

I want the heatmap color apply to columns 1-3, but not to Sum, because it takes all the juice from the heatmap and makes the columns look bland.

All I came up with right now is dividing the Sum value by 100, but that will confuse readers and will require explanations.

Is there a way to not format the Sum column, but keep its values as is?

Some code to get going:

import numpy as np
import pandas as pd
import seaborn as sns

df = pd.DataFrame(np.random.rand(3,3), columns='Cl1 Cl2 Cl3'.split())

df['Sum'] = 0
for i in df.index:
    df['Sum'].iloc[i] = np.sum(df.iloc[i])

sns.heatmap(df, annot=True, cmap='Reds')

Solution

  • use mask and add text for the masked zone using for loop.

    mask = np.zeros((3, 4))
    mask[:,3] = True
    ax = sns.heatmap(df, mask=mask, annot=True,
                 vmin=df.values[:,:3].ravel().min(),
                 vmax=df.values[:,:3].ravel().max(),
                 annot_kws={"size": 20, "color":"g"})
    
    
    for (j,i), label in np.ndenumerate(df.values):
        if i == 3:
            ax.text(i+0.5, j+0.5, label, 
                    fontdict=dict(ha='center',  va='center',
                                             color='g', fontsize=20))
    

    enter image description here


    Alternative proposal for replacing the loop:

    mask = np.zeros((3, 4))
    mask[:,3] = True
    sns.heatmap(df, mask=mask)
    sns.heatmap(df, alpha=0, cbar=False, annot=True, annot_kws={"size": 20, "color":"g"})
    

    leads to the same result