seaborn pairplot 中的自定义分箱 [英] Custom binning in seaborn pairplot

查看:70
本文介绍了seaborn pairplot 中的自定义分箱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 seaborn 的新手,我目前正在使用 pairplot 功能...以下内容

seaborn.pairplot(data,色调=类",diag_king="历史",diag_kws = {'alpha'= 0.5})

我能够实现我想要的大部分内容:从我的 pandas 数据帧 data 散布的点阵图,并根据 Class进行了单独的分布列,以及沿对角线的半透明直方图.

我已经知道,通过将 bin = [...] 传递给 diag_kws ,我可以让所有对角线图采用该合并,但我希望我的数据帧的每一列都从专用字典中进行分箱(键是列名).

是否可以使用 diag_kws 实现这一点?还是在调用 pairplot 并手动重新绑定它们之后需要分别访问每个对角线图?最有效的方法是什么?

解决方案

PairGrid 提供

I am new to seaborn, and I'm currently playing around with the pairplot functionalities... With the following

seaborn.pairplot(data,
                 hue="Class",
                 diag_king="hist",
                 diag_kws={'alpha'=0.5}
                 )

I'm able to achieve most of what I want: a grid of scatter plots from my pandas dataframe data, with separated distributions according to the Class column, and semi-transparent histograms along the diagonal.

I've figured out that by passing bin=[...] to diag_kws I can have all diagonal plots adopt that binning, but I'd like each column of my dataframe to take its binning from a dedicated dictionary (with keys the column names).

Is it possible to achieve this with diag_kws? Or do I need to access each of the diagonal plots individually after calling pairplot and rebin them manually? What's the most efficient way?

解决方案

PairGrid offers map_diag which one could use to map a custom function which changes the parameters in each call. This could look like this. Mind that one needs to take care of the order (via vars argument) to make sure the correct parameters are applied.

import matplotlib.pyplot as plt
import seaborn as sns


iris = sns.load_dataset("iris", cache=True)
col_list = ['petal_length', 'petal_width', 'sepal_length', 'sepal_width'] 
cols = iter(col_list)

bins = {'sepal_length' : 10, 'sepal_width' : 5, 
        'petal_length' : 35, 'petal_width' : 12}


def myhist(x, **kwargs):
    b = bins[next(cols)]
    plt.text(0.5,0.9, f"bins = {b}", ha="center", 
             transform=plt.gca().transAxes)
    plt.hist(x, bins=b, **kwargs)


g = sns.PairGrid(iris, vars=col_list)
g = g.map_diag(myhist)
g = g.map_offdiag(plt.scatter)

plt.show()

这篇关于seaborn pairplot 中的自定义分箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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