我的带有线性回归线的散点图未显示

My scatter plot with a linear regression line is not showing

我一直在尝试在 jupyter notebook 中用线性回归线绘制散点图,但是我的最终图没有显示出来。

%matplotlib inline
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

linear_regressor = LinearRegression()

X = fish_df.iloc[:,2].values.reshape(-1,1)
Y = fish_df.iloc[:,4].values.reshape(-1,1)

linear_regressor.fit(X, Y)
Y_pred = linear_regressor.predict(X)

plt.scatter(X,Y, color="green")

plt.plot(X, Y_pred, color="red")
plt.show()

起初我无法显示任何情节,但后来我添加了 %matplotlib inline 然后我设法得到了散点图和线性回归线,但是我想得到一个带有线性回归线的散点图。有没有另一种方法来绘制这个或者我是否遗漏了某些代码?

scatter plot
linear regression line

当我重复你的实验时,它对我有用。

我将你的数据线替换为:

import numpy as np
from random import random
X = np.arange(10).reshape(-1, 1)
y = np.arange(10).reshape(-1, 1) + np.random.rand(10).reshape(-1, 1)

然后我 运行 你在 jupyter-notebook 中的代码,并在一张图上生成了一个包含回归线和散点图的图。我建议检查您的数据 and/or 重新启动 jupyter-notebook。或者,它可能是特定版本的问题。