How to plot regression line on hexbins with Seaborn?

放肆的年华 提交于 2019-12-21 17:17:57

问题


I've finally managed to wrangle my hexbin distribution plot into something almost pretty.

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

But I'm hoping to overlay a regression line on top of it, and can't figure out how to do so. For instance, the regression line seems to occupy the marginal plot when I add regplot to the code:

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)

How to include the regression line in the body of the chart?


回答1:


You need to specify the axes you want the regplot to appear on. Here is an example, with some made-up data:

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)



来源:https://stackoverflow.com/questions/33288830/how-to-plot-regression-line-on-hexbins-with-seaborn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!