渐变的Python 3打破熊猫风格,原因是``SingleBlockManager''

渐变的Python 3打破熊猫风格,原因是``SingleBlockManager''

问题描述:

从python 2.7迁移到python 3.x,此代码中断来源:

Migrating from python 2.7 to python 3.x, This code breaks Source:

熊猫样式的背景渐变,包括行和列

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import colors

def background_gradient(s, m=None, M=None, cmap='PuBu', low=0, high=0):
    print(s.shape)
    if m is None:
        m = s.min().min()
    if M is None:
        M = s.max().max()
    rng = M - m
    norm = colors.Normalize(m - (rng * low),
                            M + (rng * high))
    normed = s.apply(norm)
    cm = plt.cm.get_cmap(cmap)
    c = normed.applymap(lambda x: colors.rgb2hex(cm(x)))
    ret = c.applymap(lambda x: 'background-color: %s' % x)
    return ret

df = pd.DataFrame([[3,2,10.3,4],[20,1,3.5,2],[5,4,6.9,1]])
df.style.apply(background_gradient, axis=None)

这是堆栈跟踪的最后一行:

This is last line of stack trace:

TypeError: ("float() argument must be a string or a number, not 'SingleBlockManager'", 'occurred at index 0')

显然,您无法在matplotlib 2.2中使用数据框调用matplotlib.colors.Normalize.

Apparently you cannot call a matplotlib.colors.Normalize with a dataframe any more in matplotlib 2.2.

解决方案是改为使用数据框的值来调用它,将normed = s.apply(norm)更改为

The solution would be to call it with the dataframe's values instead, changing normed = s.apply(norm) to

 normed = s.apply(lambda x: norm(x.values))

完整代码

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import colors

def background_gradient(s, m=None, M=None, cmap='PuBu', low=0, high=0):
    if m is None:
        m = s.min().min()
    if M is None:
        M = s.max().max()
    rng = M - m
    norm = colors.Normalize(m ,M)
    normed = s.apply(lambda x: norm(x.values))
    cm = plt.cm.get_cmap(cmap)
    c = normed.applymap(lambda x: colors.rgb2hex(cm(x)))
    ret = c.applymap(lambda x: 'background-color: %s' % x)
    return ret

df = pd.DataFrame([[3,2,10.3,4],[20,1,3.5,2],[5,4,6.9,1]])
df.style.apply(background_gradient, axis=None)

生产