我有两个变量,我正try 在Scipy Optimize中使用curve_fit
来拟合数据.它看起来不错,但左侧的红线与数据(绿点)不太匹配.我如何在curve_fit()
上加一些重物才能把左边的红线移到蓝线上呢?
以下是代码:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from pandas import DataFrame
x = [ 57, 83, 124, 141, 196, 223, 275, 302, 341, 714, 895,
1034, 1117, 1207, 1248, 1416, 1494, 1563, 1708, 1785, 1863, 2015,
2139, 2238, 2312, 2412, 2442, 2520, 2596, 2658, 2706, 2777, 2846,
2966, 3106, 3241, 3276, 3424, 3568, 3647, 3831, 3961, 4091, 4248,
4430, 4478, 4644, 4833, 5052, 6041 ]
y = [ 70, 81, 87, 91, 96, 106, 109, 114, 120, 129, 144, 162, 168,
175, 181, 184, 190, 195, 205, 213, 216, 219, 224, 226, 231, 236,
239, 247, 255, 260, 264, 269, 282, 292, 297, 304, 308, 313, 319,
322, 327, 333, 338, 341, 345, 354, 362, 364, 374, 391 ]
plt.scatter(x,y,color='green')
def func(x, a, b):
return a * np.power(x,b)
popt, pcov = curve_fit(func, x, y)
plt.plot(x, func(x, *popt), 'b-', label='fit: a=%5.3f, b=%5.3f' % tuple(popt))
popt2 = [12.6, 0.386]
plt.plot(x, func(x, *popt2), 'r-', label='fit: a=%5.3f, b=%5.3f' % tuple(popt2))
plt.semilogx()