python线性拟合曲线代码
import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize
plt.rcParams['font.sans-serif'] = [u'SimHei']
plt.rcParams['axes.unicode_minus'] = False
def f_1(x, a, b):
return a * x + b
plt.figure()
x0 = [0 ,20,40,60,80,100 ,120,140 ,160 ,180]
y0 = [0,-28,-58,-87,-115,-144,-173,-202,-231,-260]
plt.scatter(x0[:], y0[:], 3, "red")
A1, B1 = optimize.curve_fit(f_1, x0, y0)[0]
x1 = np.arange(0,185,0.1)
y1 = A1 * x1 + B1
plt.plot(x1, y1, "blue")
print(A1)
print(B1)
plt.title("应变片全桥曲线")
plt.xlabel("重量(g)")
plt.ylabel("电压(mv)单臂")
plt.show()