3

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')
Askar Akhmedov
  • 112
  • 2
  • 11

2 Answers2

3

If your goal is simply to see what this looks like in a notebook, you might be able to get by with pandas styling:

import pandas as pd
import seaborn as sns
import io

df = pd.read_fwf(io.StringIO("""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"""))


cm = sns.light_palette("green", as_cmap=True)

df.style.background_gradient(subset=['Cl1', 'Cl2', 'Cl3'], 
                             low=0, high=3, cmap=cm)

Result:

Styled dataframe

chthonicdaemon
  • 19,180
  • 2
  • 52
  • 66
3

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

Khalil Al Hooti
  • 4,207
  • 5
  • 23
  • 40
  • 1
    To get rid of the loop you might consider my edit at the end. – SpghttCd Dec 04 '18 at 08:58
  • 1
    @KhalilAlHooti Hmm, could you please explain in more detail? Because I tested exactly the above code, with initially imported `numpy`, `seaborn` and `pandas` of course, and it resulted in the same figure you posted before. – SpghttCd Dec 04 '18 at 12:06
  • 1
    yes it is working, thanks for the edit, way easier than using for loops, I missed that there are two sns.heatmaps!! thanks alot. regards, @SpghttCd – Khalil Al Hooti Dec 04 '18 at 12:10