Python 散点图的最佳拟合线
如果我不知道两个轴之间的关系(否则我可以使用scipy.optimize),有没有办法找到散点图的最佳拟合线。我的散点图是这样的 我想要一条这样的线 我需要得到最佳拟合线的点,以便进一步计算Python 散点图的最佳拟合线,python,matplotlib,Python,Matplotlib,如果我不知道两个轴之间的关系(否则我可以使用scipy.optimize),有没有办法找到散点图的最佳拟合线。我的散点图是这样的 我想要一条这样的线 我需要得到最佳拟合线的点,以便进一步计算 for j in lat : l=94*j i=l-92 for lines in itertools.islice(input_file, i, l): lines=lines.split() p.append(float(Decimal(lines[0]))) vmr.appe
for j in lat :
l=94*j
i=l-92
for lines in itertools.islice(input_file, i, l):
lines=lines.split()
p.append(float(Decimal(lines[0])))
vmr.append(float(Decimal(lines[3])))
plt.scatter(vmr, p)
这可能不是matplotlib的问题,但我认为你可以用滚动中间带对熊猫做这种事情
smoothedData = dataSeries.rolling(10, center = True).median()
实际上,你可以用任何东西做滚动中间带,但是熊猫有一个内置的功能。努比也可能 您可以使用非参数回归方法
Statsmodels
有一个实现,您可以使用它来适应自己的平滑度
有关使用Statsmodels
实现的示例,请参见此
您还可以在Seaborn
visuzalization库的regplot()
函数中使用带有关键字参数lowss=True
的实现。有关详细信息,请参阅
以下代码是使用Seaborn
和上述StackOverflow问题中的数据的示例:
import numpy as np
import seaborn as sns
sns.set_style("white")
x = np.arange(0,10,0.01)
ytrue = np.exp(-x/5.0) + 2*np.sin(x/3.0)
# add random errors with a normal distribution
y = ytrue + np.random.normal(size=len(x))
sns.regplot(x, y, lowess=True, color="black",
line_kws={"color":"magenta", "linewidth":5})