如何绘制回归线?

How to plot regression line?

import numpy as np 
import matplotlib.pyplot as plt 
import pandas as pd
import math
import csv
import seaborn as sns
import numpy.polynomial.polynomial as poly

headers = ['time', 'freq','sig_str']
df = pd.read_csv(r"Lavendershark.csv", delimiter = ',', names = headers)
sns.set()
df.pivot_table('sig_str', index='time', columns='freq').plot()
plt.ylabel("Signal Strength(MHz)")
plt.xlabel("Time(ms)")
# plt.show()

freqs = df.freq.unique()
print(freqs)

for fq in freqs:
    dffq = df[df['freq']==fq]
    print(dffq)
    X = dffq['time'].values
    Y = dffq['sig_str'].values # mean of our inputs and outputs
    x_mean = np.mean(X)
    y_mean = np.mean(Y) #total number of values
    n = len(X)  # using the formula to calculate the b1 and b0
    numerator = 0
    denominator = 0
    for i in range(n):
        numerator += (X[i] - x_mean) * (Y[i] - y_mean)
        denominator += (X[i] - x_mean) ** 2

    b1 = numerator / denominator
    b0 = y_mean - (b1 * x_mean) #printing the coefficient
    print(b1, b0)

    #plotting values 
    x_max = np.max(X) 
    x_min = np.min(X) #calculating line values of x and y
    x = np.linspace(x_min, x_max, 1000)
    y = b0 + b1 * x #plotting line 
    plt.plot(x, y, color='#00ff00') #plot the data point
    plt.legend()

    coefs = np.polyfit(X, Y, 3)
    x_new = np.linspace(X[0], X[-1], num=len(X)*10)
    ffit = np.poly1d(coefs)
    plt.plot(x_new, ffit(x_new),color='#f2411b')
    plt.legend()


plt.show()

我想绘制此图,其中包含数据点以及数据集的线性和多项式回归线。

但我不知道如何 select/remove 下图中的线条,以便获得所需的结果

使用plt.scatter(),即

plt.scatter(x, y, color='#00ff00')

而不是

plt.plot(x, y, color='#00ff00')

为了数据(不是为了合身)。拟合散点图示例:

import numpy as np
from numpy.polynomial.polynomial import polyfit
import matplotlib.pyplot as plt

n=50
x = np.linspace(0, 10, n)
y = 5 * x + 10 + (np.random.random(n) - 0.5) * 5
b, m = polyfit(x, y, 1)

plt.scatter(x, y,  marker='.')
plt.plot(x, b + m*x, linestyle='-')
plt.show()