奇怪的cmap background_gradient行为 [英] Strange cmap background_gradient behavior

查看:289
本文介绍了奇怪的cmap background_gradient行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现并正在使用@mrandrewandrade 绝佳答案,使用样式熊猫来显示波士顿住房数据的相关系数.当我注意到background_gradient()中使用的颜色映射未正确计算CHAS数据时,在iPython笔记本中打开了dataframe. 看起来B数据中的某些值也会受到影响.

I found and am using @mrandrewandrade great answer to display the correlation coefficients of the boston housing data using a styled panda dataframe in an iPython notebook when I've noticed that the color mapping used in the background_gradient() isn't calculating correctly for the CHAS data. It also looks like some of the values in the B data are affected as well.

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

Its correct in the axis passed into the background_gradient(cmap, axis=1) command but not the other axis. The row axis will work is you change that line to axis=0. All the other table cells seem to calculate fine.

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

Can someone help figure out the problem, I'm stumped as to what is happening and how to avoid it?

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()

# 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())

为突出显示此问题,此处绘制的数据与海上热图相同:

To help highlight the issue here is the same data plotted as a seaborn heatmap:

# 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')

推荐答案

我通过遇到同样的问题发现了这个问题. 此答案解决了我的问题.

I found this question by having the same issue. This answer solved my problem.

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

In short, it seems there is no way to use the actual backgroud_gradient DataFrame method, but is not too complicated to achieve the effect you're looking for with a custom function.

这篇关于奇怪的cmap background_gradient行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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