# Python图像处理之直线和曲线的拟合与绘制【curve_fit()应用】

```# -*- coding:utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize
#直线方程函数
def f_1(x, A, B):
return A*x + B
#二次曲线方程
def f_2(x, A, B, C):
return A*x*x + B*x + C
#三次曲线方程
def f_3(x, A, B, C, D):
return A*x*x*x + B*x*x + C*x + D
def plot_test():
plt.figure()
#拟合点
x0 = [1, 2, 3, 4, 5]
y0 = [1, 3, 8, 18, 36]
#绘制散点
plt.scatter(x0[:], y0[:], 25, "red")
#直线拟合与绘制
A1, B1 = optimize.curve_fit(f_1, x0, y0)[0]
x1 = np.arange(0, 6, 0.01)
y1 = A1*x1 + B1
plt.plot(x1, y1, "blue")
#二次曲线拟合与绘制
A2, B2, C2 = optimize.curve_fit(f_2, x0, y0)[0]
x2 = np.arange(0, 6, 0.01)
y2 = A2*x2*x2 + B2*x2 + C2
plt.plot(x2, y2, "green")
#三次曲线拟合与绘制
A3, B3, C3, D3= optimize.curve_fit(f_3, x0, y0)[0]
x3 = np.arange(0, 6, 0.01)
y3 = A3*x3*x3*x3 + B3*x3*x3 + C3*x3 + D3
plt.plot(x3, y3, "purple")
plt.title("www.jb51.net test")
plt.xlabel('x')
plt.ylabel('y')
plt.show()
return
plot_test()

```

```def f_gauss(x, A, B, C, sigma):
return A*np.exp(-(x-B)**2/(2*sigma**2)) + C

```

PS：这里再为大家推荐两款相似的在线工具供大家参考：

http://tools.jb51.net/jisuanqi/create_fun

http://tools.jb51.net/jisuanqi/fun_draw