Home > Blockchain >  How to normalize coloring of data with seaborn in pandas?
How to normalize coloring of data with seaborn in pandas?

Time:10-07

I got data like you can see in picture 1, because I have value 0 and rest is much bigger (values are between 0 and 100). I would like to get data like is show in picture 2. How to solve this problem?

This is minimal reproducible code.

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib import colors
index = pd.MultiIndex.from_product([[2019, 2020], [1, 2]],
                                   names=['year', 'visit'])
columns = pd.MultiIndex.from_product([['Group1', 'Group2', 'Group3'], ['value1', 'value2']],
                                     names=['subject', 'type'])

data = np.round(np.random.randn(4, 6), 1)
data[:, ::2] *= 20
data  = 50

rdata = pd.DataFrame(data, index=index, columns=columns)

cc = sns.light_palette("red", as_cmap=True)
cc.set_bad('white')

def my_gradient(s, cmap):
    return [f'background-color: {colors.rgb2hex(x)}'
            for x in cmap(s.replace(np.inf, np.nan))]
       

styler = rdata.style
red = styler.apply(
        my_gradient,
        cmap=cc, 
        subset=rdata.columns.get_loc_level('value1', level=1)[0],
        axis=0)

styler

Picture 1

enter image description here

Picture 2

enter image description here

CodePudding user response:

You need to normalize. Usually, in matplotlib, a norm is used, of which plt.Normalize() is the most standard one.

The updated code could look like:

my_norm = plt.Normalize(0, 100)

def my_gradient(s, cmap):
    return [f'background-color: {colors.rgb2hex(x)}'
            for x in cmap(my_norm(s.replace(np.inf, np.nan)))]

CodePudding user response:

You can normalize you data with the following equation (x-min)/(max-min). So to apply this to your dataframe you could use something like the following:

result = pd.DataFrame()
for i,row in df.iterrows():
    hold = {}
    for h in df:
        hold[h] = (row[h]-df[h].min())/(df[h].max()-df[h].min())
    result = result.append(hold,ignore_index=True)
  • Related