from scipy.optimize import curve_fit def f(x, A, B): # this is your 'straight line' y=f(x) return A*x + B popt, pcov = curve_fit(f, x, y) # your data x, y to fit