散点图拟合
import matplotlib.pyplot as plt
import numpy as np
x = [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)
p1 = np.poly1d(z1)
print(p1)
yvals=p1(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)
plt.title('polyfitting')
plt.show()
运行效果如下:
运行结果可得出拟合多项式