首页 > 解决方案 > 使用 Seaborn PairGrid、regplot 为不同变量绘制不同的回归函数

问题描述

我的问题是,如何在 seaborn PairGrid 中绘制回归,这取决于绘制的变量,而不是上/下/对角位置?例如,我有tips数据集,我相信'size'无论其他变量如何,它都作为二阶多项式相关,即。我想要的pairgrid中的整个行/列,但没有别的。但是,我只能将这种相关性映射到所有 plots的上/下三角形,如下所示:

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可以吗?如果我走得更远,如果我想检查/绘制例如之间的指数相关性怎么办。'tip''total_bill'在pairgrid之内,这可能吗?我该怎么做?

我知道我可以将这个特定案例放在外面单独绘制或使用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. -- 添加色调

标签: pythonmatplotlibseaborn

解决方案


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

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)

在此处输入图像描述

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

在这种情况下,您必须对数据的每个子集进行回归并在相同的轴上绘图。

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)

在此处输入图像描述


推荐阅读