如何使用 Seaborn 在 hexbins 上绘制回归线?

How to plot regression line on hexbins with Seaborn?

我终于设法将我的 hexbin 分布图整理成几乎漂亮的东西。

import seaborn as sns

x = req.apply_clicks
y = req.reqs_wordcount

sns.jointplot(x, y, kind="hex", color="#5d5d60",
         joint_kws={'gridsize':40, 'bins':'log'})

但我希望在其上叠加一条回归线,但不知道该怎么做。例如,当我在代码中添加 regplot 时,回归线似乎占据了边缘图:

x = req.apply_clicks
y = req.reqs_wordcount
z = sns.jointplot(x, y, kind="hex", color="#5d5d60",
         joint_kws={'gridsize':40, 'bins':'log'})
sns.regplot(x, y, data=z, color="#5d5d60", scatter=False)

如何在图表主体中包含回归线?

您需要指定希望 regplot 出现的轴。这是一个示例,其中包含一些虚构的数据:

import seaborn as sns
import numpy as np

x = 0.3 + 0.3 * np.random.randn(10000)
y = 0.1 - 0.2 * x + 0.1 * np.random.randn(10000)
mask = (y > 0) & (x > 0)
x, y = x[mask], y[mask]

g = sns.jointplot(x, y, kind="hex", color="#5d5d60",
                  joint_kws={'gridsize':40, 'bins':'log'})
sns.regplot(x, y, ax=g.ax_joint, scatter=False)