如何使用`lmplot`绘制线性回归而不拦截?

Eas*_*sun 21 python linear-regression seaborn

lmplot在seaborn拟合回归模型的截距.但是,有时我想要在没有截距的情况下拟合回归模型,即通过原点进行回归.

例如:

In [1]: import numpy as np
   ...: import pandas as pd
   ...: import seaborn as sns
   ...: import matplotlib.pyplot as plt
   ...: import statsmodels.formula.api as sfa
   ...: 

In [2]: %matplotlib inline
In [3]: np.random.seed(2016)
In [4]: x = np.linspace(0, 10, 32)
In [5]: y = 0.3 * x + np.random.randn(len(x))
In [6]: df = pd.DataFrame({'x': x, 'y': y})
In [7]: r = sfa.ols('y ~ x + 0', data=df).fit()
In [8]: sns.lmplot(x='x', y='y', data=df, fit_reg=True)
Out[8]: <seaborn.axisgrid.FacetGrid at 0xac88a20>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这个数字我想要的是:

In [9]: fig, ax = plt.subplots(figsize=(5, 5))
   ...: ax.scatter(x=x, y=y)
   ...: ax.plot(x, r.fittedvalues)
   ...: 
Out[9]: [<matplotlib.lines.Line2D at 0x5675a20>]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Lin*_*Lin -5

这符合您的目的吗?

sns.lmplot(x='x', y='y', data=df, fit_reg=False)
Run Code Online (Sandbox Code Playgroud)