擅长:python、mysql、java
<p>当前不能同时为这两行/列设置<code>background_gradient</code>,如<a href="https://stackoverflow.com/users/6207849/nickil-maveli">Nickil Maveli</a>所指。诀窍是定制<a href="https://github.com/pandas-dev/pandas/blob/29d81f3df81eb0a4d077ae1317df74d509cdc446/pandas/formats/style.py#L817" rel="noreferrer">pandas function background_gradient</a>:</p>
<pre><code>import pandas as pd
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 = pd.DataFrame([[3,2,10,4],[20,1,3,2],[5,4,6,1]])
df.style.apply(background_gradient,
cmap='PuBu',
m=df.min().min(),
M=df.max().max(),
low=0,
high=0.2)
</code></pre>