奇怪的cmap background_gradient行为

时间:2018-08-28 18:31:48

标签: python pandas

当我注意到{{1} }对于background_gradient()数据无法正确计算。 看起来CHAS数据中的某些值也会受到影响。

它在传递给B命令的轴上正确,但在另一轴上正确。当您将该行更改为background_gradient(cmap, axis=1)时,行轴将起作用。所有其他表单元格似乎都可以计算。

有人可以帮忙解决问题吗,我对正在发生的事情以及如何避免它感到困惑?

axis=0

great answer

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://stackoverflow.com/a/42323184/1215012
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())

enter image description here

1 个答案:

答案 0 :(得分:1)

我通过遇到同样的问题发现了这个问题。 This answer解决了我的问题。

简而言之,似乎无法使用实际的backgroud_gradient DataFrame方法,但是使用自定义函数实现所需的效果并不太复杂。

相关问题