简体   繁体   English

使用 seaborn regplot 扩展回归线

[英]Extended regression lines with seaborn regplot

I searched but I did not find the answer regrading the seaborn library.我进行了搜索,但没有找到重新分级 seaborn 库的答案。 I also checked the documentation for lmplot() and regplot() , but did not find either.我还检查了lmplot()regplot()的文档,但也没有找到。 Is it possible to extend and control the length of regression lines?是否可以扩展和控制回归线的长度? By default seaborn fits the length of regression line according to the length of x axis.默认情况下,seaborn 根据 x 轴的长度拟合回归线的长度。 Another option is to use argument truncate=True - that would limit the regression line only to the extent of data.另一种选择是使用参数truncate=True - 这会将回归线限制在数据范围内。 Other options?其他选择?

In my example I want the lower regression line to be extended down till x=0.在我的示例中,我希望将下回归线向下延伸至 x=0。 And the upper line extended till the intersection with the lower one.而上面的那条线一直延伸到与下面那条线的交点。

例子

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

file = 'cobbles.csv'
df = pd.read_csv(file, sep=',')

sns.regplot(x='downward_temp', y='downward_heat', data=df, ci=None)
sns.regplot(x='upward_temp', y='upward_heat', data=df, ci=None, order=2)


plt.xlim([0,25])
plt.ylim([0,100])
plt.show()

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.如果你之前知道要策划你的X限制,你可以set_xlim之前调用该轴regplot和seaborn然后将延长回归线和CI超过XLIM的范围。

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

file = 'cobbles.csv'
df = pd.read_csv(file, sep=',')

fig, ax = plt.subplots()

xlim = [0,25]
ax.set_xlim(xlim)

sns.regplot(x='downward_temp', y='downward_heat', data=df, ci=None, ax=ax)
sns.regplot(x='upward_temp', y='upward_heat', data=df, ci=None, order=2, ax=ax)

ax.set_ylim([0,100])
plt.show()

Short answer: You just have to add plt.xlim(start,end) before your Seaborn plots.简短回答:您只需要Seaborn 绘图之前添加plt.xlim(start,end)


I guess it might make more sense for Seaborn to automatically determine the length from the plot limits.我想 Seaborn 从绘图限制自动确定长度可能更有意义。

The same issue brought me here, and @Serenity's answer inspired me that something like xlims = ax.get_xlim() might help.同样的问题把我带到了这里,@Serenity 的回答启发了我像xlims = ax.get_xlim()这样的东西可能会有所帮助。

May try fixing and commit a change to Seaborn afterwards.之后可以尝试修复并提交对 Seaborn 的更改。

You have to use scipy.stats.linregress to calculate linear regression function like seaborn do.你必须像 seaborn 一样使用scipy.stats.linregress来计算线性回归函数。 Then you have to generate x array to cover new x axis limits of canvas and plot on it extended regression line.然后您必须生成 x 数组以覆盖画布的新 x 轴限制并在其上绘制扩展回归线。 For details looks at the example:有关详细信息,请查看示例:

import numpy as np; np.random.seed(8)

import seaborn as sns
import matplotlib.pylab as plt
import scipy.stats

# test data
mean, cov = [4, 6], [(1.5, .7), (.7, 1)]
x, y = np.random.multivariate_normal(mean, cov, 80).T
ax = sns.regplot(x=x, y=y, color="g")

# extend the canvas
plt.xlim([0,20])
plt.ylim([0,15])

# calculate linear regression function
slope, intercept, r_value, p_value, std_err = \
 scipy.stats.linregress(x=x,y=y)

# plot the regression line on the extended canvas
xlims = ax.get_xlim()
new_x = np.arange(xlims[0], xlims[1],(xlims[1]-xlims[0])/250.)
ax.plot(new_x, intercept + slope *  new_x, color='g', linestyle='-', lw = 2.5)

plt.show()

在此处输入图片说明

Setting truncate to False does the job.将 truncate 设置为 False 就可以了。 Adding this answer here, might help someone在此处添加此答案,可能会对某人有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM