1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python散点图拟合曲线-python曲线拟合

python散点图拟合曲线-python曲线拟合

时间:2024-04-16 19:36:00

相关推荐

python散点图拟合曲线-python曲线拟合

/s/blog_aed5bd1d0102vid7.html

1.多项式拟合范例:

import matplotlib.pyplot as plt

import numpy as np

#潘海东,/1/13

x = np.arange(1, 17, 1)

y = np.array([4.00, 6.40, 8.00, 8.80, 9.22, 9.50, 9.70, 9.86, 10.00, 10.20, 10.32, 10.42, 10.50, 10.55, 10.58, 10.60])

z1 = np.polyfit(x, y, 3)#用3次多项式拟合

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()

plt.savefig('p1.png')

2.指定函数拟合

#潘海东,/1/13

#使用非线性最小二乘法拟合

import matplotlib.pyplot as plt

from scipy.optimize import curve_fit

import numpy as np

#用指数形式来拟合

x = np.arange(1, 17, 1)

y = np.array([4.00, 6.40, 8.00, 8.80, 9.22, 9.50, 9.70, 9.86, 10.00, 10.20, 10.32, 10.42, 10.50, 10.55, 10.58, 10.60])

def func(x,a,b):

return a*np.exp(b/x)

popt, pcov = curve_fit(func, x, y)

a=popt[0]#popt里面是拟合系数,读者可以自己help其用法

b=popt[1]

yvals=func(x,a,b)

plot1=plt.plot(x, y, '*',label='original values')

plot2=plt.plot(x, yvals, 'r',label='curve_fit values')

plt.xlabel('x axis')

plt.ylabel('y axis')

plt.legend(loc=4)#指定legend的位置,读者可以自己help它的用法

plt.title('curve_fit')

plt.show()

plt.savefig('p2.png')

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。