python - 如何在散点图中可视化非线性关系

标签 python matplotlib curve-fitting statsmodels smoothing

我想直观地探索两个变量之间的关系。这种关系的函数形式在像这样的密集散点图中是不可见的:

scatter plot

如何在 Python 中向散点图添加低平滑度?

或者您有任何其他视觉探索非线性关系的建议吗?

我尝试了以下但它没有正常工作(借鉴 Michiel de Hoon 中的示例):

import numpy as np
from statsmodels.nonparametric.smoothers_lowess import lowess
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))
plt.scatter(x,y,color='cyan')

# calculate a smooth curve through the scatter plot
ys = lowess(x, y)
_ = plt.plot(x,ys,'red',linewidth=1)

# draw the true values for comparison
plt.plot(x,ytrue,'green',linewidth=3)

lowess

较低的平滑器(红线)很奇怪。

编辑:

以下矩阵还包括低平滑器(取自 CV 上的 this question): enter image description here

有人有这种图表的代码吗?

最佳答案

你也可以使用 seaborn :

import numpy as np
import seaborn as sns

x = np.arange(0, 10, 0.01)
ytrue = np.exp(-x / 5) + 2 * np.sin(x / 3)
y = ytrue + np.random.normal(size=len(x))

sns.regplot(x, y, lowess=True)

enter image description here

关于python - 如何在散点图中可视化非线性关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23784399/

相关文章:

python - 将闭合曲线拟合到一组离散点并找到其周长

python - Numpy 非零/flatnonzero 索引顺序; bool 索引中返回元素的顺序

python - 如何在 python 中打印 sqlite3 的输出

python - float() 参数必须是字符串或数字,而不是 'zip'

python - 使用 matplotlib.pyplot.imshow() 时如何确定颜色?

用于全局变量的洛伦兹拟合的 Matlab 函数

Python:降低 RGB 颜色的亮度

python - 使用 scikit-learn 的一次性编码

python - 如何使用Intel RealSense D435创建高度图

python - 为什么 scipy.optimize.curve_fit 没有产生最适合我的点的线?