繁体   English   中英

Plot 不同变量的不同回归函数与 Seaborn PairGrid, regplot

[英]Plot different regression functions for different variables with Seaborn PairGrid, regplot

我的问题是,我如何在 seaborn PairGrid 中进行 plot 回归,这取决于绘制的变量,而不是上/下/对角线 Z4757FE07FD492A8BE0EA6A760DEZ63? 例如,我有tips数据集,并且我相信'size'作为二阶多项式相关,而与其他变量无关,即。 我想要的pairgrid中的整个行/列,但没有别的。 但是,我只能做的是 map 这种与所有地块的上/下三角形的相关性,如下所示:

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

smoke = sns.PairGrid(tips, vars=['total_bill', 'tip','size'])
smoke.map_upper(sns.regplot, color = 'k', order=2)
smoke.map_diag(sns.kdeplot)
smoke.map_lower(sns.regplot, color = 'b')

图。1

seaborn 可以吗? 如果我 go 更进一步,如果我想检查/绘制例如之间的指数相关性怎么办。 'tip''total_bill'就在pairgrid中,这可能吗? 我该怎么做?

我知道我可以把这个特定的情况放在外面,然后单独使用 plot 或使用GridSpec但我想知道是否有更简单的方法。 谢谢


编辑(26.4.):另一个问题是如何在这个设置中使用hue 如果我简单地使用:

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")
vars = ['total_bill', 'tip','size']

smoke = sns.PairGrid(tips, vars=vars, hue='smoker')
smoke.map_upper(plt.scatter)
smoke.map_diag(sns.kdeplot)
smoke.map_lower(plt.scatter)

# Add 2nd order polynomial regression to the 'size' column
for ax,y in zip(smoke.axes[:2,2],vars):
    sns.regplot(ax=ax, data=tips, x='size', y=y, order=2, scatter=False)
    ax.set_ylabel('')
    ax.set_xlabel('')

# Add logarithmic regression
sns.regplot(ax=smoke.axes[2,0], data=tips, x="total_bill", y='size', logx=True, scatter=False)

它做我想要的,即适合对数回归,但很奇怪。 它将蓝色仅用于第一行,橙色仅用于第二行,然后为第一列创建绿色,最后一行如下图所示。 所以我的问题是如何解决它以及为什么它首先发生。 hue是否创建了一组新的axes ,然后需要对其进行迭代?

图 2. -- 添加色调

PairGrid只允许您 map 对角线、非对角线以及上下三角形。 如果您想对绘图进行更细粒度的控制,可以使用PairGrid.axes (二维数组)访问各个轴 object:

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")
vars = ['total_bill', 'tip','size']

smoke = sns.PairGrid(tips, vars=vars)
smoke.map_upper(plt.scatter, color = 'k')
smoke.map_diag(sns.kdeplot)
smoke.map_lower(plt.scatter, color = 'b')

# Add 2nd order polynomial regression to the 'size' column
for ax,y in zip(smoke.axes[:2,2],vars):
    sns.regplot(ax=ax, data=tips, x='size', y=y, order=2, color='k', scatter=False)

# Add logarithmic regression
sns.regplot(ax=smoke.axes[2,0], data=tips, x="total_bill", y='size', logx=True, color='b', scatter=False)

在此处输入图像描述

编辑:适用于色调分割的解决方案

在这种情况下,您必须在同一轴上对数据的每个子集和 plot 进行回归。

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")
vars = ['total_bill', 'tip','size']
hue_col = 'smoker'
hue_order=['Yes','No']

smoke = sns.PairGrid(tips, vars=vars, hue='smoker', hue_order=hue_order)
smoke.map_upper(plt.scatter)
smoke.map_diag(sns.kdeplot)
smoke.map_lower(plt.scatter)

# Add 2nd order polynomial regression to the 'size' column
for ax,y in zip(smoke.axes[:2,2],vars):
    for hue in hue_order:
        sns.regplot(ax=ax, data=tips.loc[tips[hue_col]==hue], x='size', y=y, order=2, scatter=False)
    ax.set_ylabel('')
    ax.set_xlabel('')

# Add logarithmic regression
for hue in hue_order:
    sns.regplot(ax=smoke.axes[2,0], data=tips.loc[tips[hue_col]==hue], x="total_bill", y='size', logx=True, scatter=False)

在此处输入图像描述

是的,这是可能的,因为您可以分别指定 x 和 y 变量,例如

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

smoke = sns.PairGrid(tips, x_vars=['total_bill', 'tip','size'], y_vars=['size'])
smoke.map(sns.regplot, color = 'k', order=2)
smoke.map_diag(sns.kdeplot)

配对图示例

对于 plot 各种回归函数,您必须单独访问每个轴(子图)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM