如何在Seaborn的六边形图上绘制回归线?

6

我终于成功地将我的六角形分布图整理成了一些几乎漂亮的东西。

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

Seaborn Hexbin

但我希望在上面叠加一个回归线,但是我不知道该如何实现。例如,当我在代码中添加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

如何在图表正文中包含回归线?


1
当然可以,使用regplot并将scatter=False - mwaskom
@mwaskom 我似乎无法让jointplot和regplot协同工作。我编辑了我的问题。非常感谢您的帮助。 - samthebrand
1
你需要指定你想要回归线绘制在哪些轴上。 - mwaskom
1个回答

7
您需要指定您想要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)

enter image description here


非常感谢。这很有帮助。现在如果我能够改变边际图的样式以匹配主图表(或完全删除它们),那就好了。 - samthebrand
关于图形样式,Seaborn文档的这一部分可能会有所帮助。您可以尝试使用sns.set_style("white") - jakevdp
这张图中的 Pearson R 是从哪里来的? - naught101

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接