I get the following error when trying to plot modelled data: ValueError: Length of passed values is 2, index implies 9. How to fix this?
I get the following error when trying to plot modelled data: ValueError: Length of passed values is 2, index implies 9. How to fix this?
我正在尝试绘制观察到的和建模的数据。这是数据框的示例,位于代码下方。
我收到此错误#### ValueError:传递值的长度为 2,索引意味着 9####。
我不知道如何绘制建模数据。
all = [treatment1, treatment2]
[ x y intercept
0 25 0.171144 1.6
1 50 1.859164 1.6
2 100 4.407867 1.6
x y intercept
14 25 0.997440 1.3
15 50 1.823077 1.3
16 100 4.412220 1.3]
def model(x, slope):
return (slope*x) + intercept
def func_fit(x):
return model(x,
popt)
from scipy.optimize import curve_fit
for g in all: #this loop allows to fit the model for each treatment
popt = curve_fit(model, g['x'], g['y'])
new_row = {'treatment':treatment, 'slope': popt[0], 'intercept':intercept}
results=results.append(new_row, ignore_index=True)
plt.plot(g['x'], g['y'], '.', label='data')
plt.plot(g['x'], func_fit(g['x']), 'r-', tuple(popt)) # this is the line that gives the error.
#### ValueError: Length of passed values is 2, index implies 9####
#I tried to overcome the error but what I get with these lines below is no line
#t= np.linspace(0,200,1)
#a = model(t,results.iloc[0,1])
#plt.plot(t, a, 'r') # plotting t, a separately
这些是观察结果,我也想绘制模型数据:
我假设您想绘制模型学到的函数?
如果是这种情况,这里是使用 Official docs scipy.optimize.curve_fit:
中的代码片段的简单实现
data = [
[25, 0.171144, 1.6],
[50, 1.859164, 1.6],
[100, 4.407867, 1.6]]
df = pd.DataFrame(data, columns=['x', 'y', 'intercept'])
def model(x, slope, intercept):
return (slope*x) + intercept
xdata=df['x']
ydata=df['y']
popt, _ = curve_fit(model, xdata, ydata)
plt.plot(xdata, ydata, 'o', label='data')
plt.plot(xdata, model(xdata, *popt), 'r-',
label='fit: slope=%5.3f, intercept=%5.3f' % tuple(popt))
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
导致这个结果:
我正在尝试绘制观察到的和建模的数据。这是数据框的示例,位于代码下方。 我收到此错误#### ValueError:传递值的长度为 2,索引意味着 9####。 我不知道如何绘制建模数据。
all = [treatment1, treatment2]
[ x y intercept
0 25 0.171144 1.6
1 50 1.859164 1.6
2 100 4.407867 1.6
x y intercept
14 25 0.997440 1.3
15 50 1.823077 1.3
16 100 4.412220 1.3]
def model(x, slope):
return (slope*x) + intercept
def func_fit(x):
return model(x,
popt)
from scipy.optimize import curve_fit
for g in all: #this loop allows to fit the model for each treatment
popt = curve_fit(model, g['x'], g['y'])
new_row = {'treatment':treatment, 'slope': popt[0], 'intercept':intercept}
results=results.append(new_row, ignore_index=True)
plt.plot(g['x'], g['y'], '.', label='data')
plt.plot(g['x'], func_fit(g['x']), 'r-', tuple(popt)) # this is the line that gives the error.
#### ValueError: Length of passed values is 2, index implies 9####
#I tried to overcome the error but what I get with these lines below is no line
#t= np.linspace(0,200,1)
#a = model(t,results.iloc[0,1])
#plt.plot(t, a, 'r') # plotting t, a separately
这些是观察结果,我也想绘制模型数据:
我假设您想绘制模型学到的函数?
如果是这种情况,这里是使用 Official docs scipy.optimize.curve_fit:
data = [
[25, 0.171144, 1.6],
[50, 1.859164, 1.6],
[100, 4.407867, 1.6]]
df = pd.DataFrame(data, columns=['x', 'y', 'intercept'])
def model(x, slope, intercept):
return (slope*x) + intercept
xdata=df['x']
ydata=df['y']
popt, _ = curve_fit(model, xdata, ydata)
plt.plot(xdata, ydata, 'o', label='data')
plt.plot(xdata, model(xdata, *popt), 'r-',
label='fit: slope=%5.3f, intercept=%5.3f' % tuple(popt))
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
导致这个结果: