I would like to render a DataFrame as in Excel when using the Graded color formatting:
I'm not able to get similar result with Pandas (v0.23.0
) style:
import pandas as pd
import numpy as np
shape = (3,3)
np.random.seed(0)
df=pd.DataFrame(np.random.uniform(0.,144, size=9).reshape(shape))
df.iloc[0,0] = np.nan
df.iloc[1,1] = 144
df.iloc[0,2] = 0
df[3] = 144
df.style.background_gradient(cmap='RdYlGn').highlight_null('white')
This is what I get:
Any idea?
Thanks, Greg
EDIT
This solution worked:
import matplotlib.pyplot as plt
from matplotlib import colors
def background_gradient(s, m, M, cmap='PuBu', low=0, high=0):
rng = M - m
norm = colors.Normalize(m - (rng * low),
M + (rng * high))
normed = norm(s.values)
c = [colors.rgb2hex(x) for x in plt.cm.get_cmap(cmap)(normed)]
return ['background-color: %s' % color for color in c]
df.style.apply(background_gradient,
cmap='RdYlGn',
m=df.min().min(),
M=144).highlight_null('white')