散点图拟合
import matplotlib.pyplot as plt
import numpy as npx = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
y = np.array([5760, 3600, 1620, 1260, 1080, 900, 1080, 1800, 3060, 4680, 2880, 5040, 4140, 5580, 5040, 4860, 3780,3420, 4860, 3780, 4860, 5220, 4860, 3600])
z1 = np.polyfit(x, y, 4) # 用4次多项式拟合
p1 = np.poly1d(z1)
print(p1) # 在屏幕上打印拟合多项式
yvals=p1(x) # 也可以使用yvals=np.polyval(z1,x)
plot1=plt.plot(x, y, '*',label='original values')
plot2=plt.plot(x, yvals, 'r',label='polyfit values')
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.legend(loc=4) # 指定legend的位置,读者可以自己help它的用法
plt.title('polyfitting')
plt.show()
运行效果如下:
运行结果可得出拟合多项式
欢迎加群:620139909














![[Python] 散点图(二维散点图、三维散点图、散点图矩阵)](https://img-blog.csdnimg.cn/07fecaa21d2d432f948829eee5e8b881.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA56a-5pyo6aG1,size_20,color_FFFFFF,t_70,g_se,x_16)

