渐变的Python 3打破 pandas 风格,原因是``SingleBlockManager'' [英] Python 3 break pandas style for gradients, 'SingleBlockManager' is cause

查看:119
本文介绍了渐变的Python 3打破 pandas 风格,原因是``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)

生产

这篇关于渐变的Python 3打破 pandas 风格,原因是``SingleBlockManager''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆