散点图拟合
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) # 用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()
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/215993.html原文链接:https://javaforall.net
