尝试绘制建模数据时出现以下错误:ValueError: 传递值的长度为 2,索引意味着 9。如何解决此问题?
我正在尝试绘制观察到的和建模的数据。这是数据框的示例,位于代码下方。 我收到此错误#### 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
这些是观察结果,我也想绘制建模数据:
I am trying to plot observed and modelled data. This is an example of dataframe and below the code.
I get this error #### ValueError: Length of passed values is 2, index implies 9####.
I don't know how else to plot the modelled data.
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
These are the observations and I would like to plot the modelled data as well:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您想绘制模型学到的函数?
如果是这种情况,这里是使用 官方文档 scipy.optimize.curve_fit:
导致此结果:

I assume you want to plot the function your model learned?
If this is the case here is a simple implementation using the code snippets from the Official docs scipy.optimize.curve_fit:
Leads to this result:
