奇怪的cmap背景渐变行为

6
我发现并正在使用@mrandrewandrade的很棒的答案,在iPython笔记本中使用样式化panda数据框显示波士顿房屋数据的相关系数时,我注意到background_gradient()中使用的颜色映射对于CHAS数据计算不正确。同时,B数据中的某些值也受到影响。
如果传递给background_gradient(cmap, axis=1)命令的轴是正确的,则其可以正常工作,但是另一个轴则不行。如果将该行更改为axis=0,则行轴将正常工作。所有其他表单元格似乎都可以正常计算。
有人可以帮助找出问题所在吗? 我被卡住了,不知道发生了什么以及如何避免它。
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# load Boston housing data into a dataframe
from sklearn.datasets import load_boston
boston = load_boston()
bos = pd.DataFrame(boston.data, columns=boston.feature_names)
bos['MEDV'] = boston.target
bos.head()

enter image description here

# using a styled panda's dataframe from https://dev59.com/-VkS5IYBdhLWcg3w05eT#42323184
cmap = 'coolwarm'

def magnify():
    return [dict(selector="th", props=[("font-size", "7pt")]),
            dict(selector="td", props=[('padding', "0em 0em")]),
            dict(selector="th:hover", props=[("font-size", "12pt")]),
            dict(selector="tr:hover td:hover", 
                 props=[('max-width', '200px'), ('font-size', '12pt')])
]

corr.style.background_gradient(cmap, axis=1)\
    .set_properties(**{'max-width': '80px', 'font-size': '10pt'})\
    .set_caption("Hover to magify")\
    .set_precision(2)\
    .set_table_styles(magnify())

为了更好地突出问题,这里将相同的数据绘制成seaborn热图:

enter image description here

# calculating and plotting the correlation coeff's using a seaborn heatmap
corr = bos.corr()
sns.heatmap(corr, xticklabels=corr.columns, yticklabels=corr.columns, cmap='coolwarm')

enter image description here

1个回答

2
我通过遇到同样的问题找到了这个问题。此答案 解决了我的问题。
简而言之,似乎没有办法使用实际的 background_gradient DataFrame 方法,但使用自定义函数实现你想要的效果并不太复杂。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接