Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot regression line on hexbins with Seaborn?

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

Seaborn Hexbin

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)

regplot in marginal plot

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

like image 834
samthebrand Avatar asked Oct 22 '15 19:10

samthebrand


People also ask

How do you plot a regression line in Seaborn?

Regression plots in seaborn can be easily implemented with the help of the lmplot() function. lmplot() can be understood as a function that basically creates a linear model plot. lmplot() makes a very simple linear regression plot.It creates a scatter plot with a linear fit on top of it.

How do you extend a regression line in Seaborn?

If you know your x limits prior to plotting, you can set_xlim for the axis before calling regplot and seaborn will then extend the regression line and the CI over the range of xlim. You also need truncate=False in the regplot(...) call for this to work.


1 Answers

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)

enter image description here

like image 60
jakevdp Avatar answered Oct 11 '22 05:10

jakevdp