【问题标题】:Tuple indices must be integers not tuple, matplot元组索引必须是整数而不是元组,matplotlib
【发布时间】:2016-04-08 18:43:12
【问题描述】:

我正在尝试编写一个程序,该程序将使用不同的方式(欧拉、龙格...)并使用内置函数 scipy.integrate.odeint 集成一个函数。

一切都好,我得到了正确的结果,但我还需要用结果创建一个图表,这就是一切都出错的时候。 对于 odeint 函数,我无法绘制图形。 这是我的代码和错误,希望有人能够帮助我。

def odeint(phi, t0tf, Y0, N):

    T6=numpy.zeros((N+1)) 
    T6[0]=t0tf[0]
    h=(t0tf[1]-t0tf[0])/N

    for i in range (N):
        T6[i+1]=T6[i]+h

    def f(t,x):
        return phi(x,t)

    Y6 = scipy.integrate.odeint(f,Y0,T6, full_output=True)

    return Y6

Y6 = edo.odeint(phi, t0tf, Y0, N)
T6Y6 = numpy.hstack([Y6])
print("Solutions Scipy :")
print()
print(T6Y6)
print()

mpl.figure("Courbes")
mpl.plot(Y6[0:N,0],Y6[0:N,1],color="yellow",label="Isoda")
mpl.show()

错误是:

 mpl.plot(Y6[0:N,0],Y6[0:N,1],color="yellow",label="Isoda")

 TypeError: tuple indices must be integers, not tuple

提前致谢(PS:我是法国人,所以我的句子可能有点不稳定)

【问题讨论】:

  • 能否请您包含完整的回溯并包含足够的信息,以便我们可以复制-粘贴-运行您的示例?

标签: numpy matplotlib graph tuples


【解决方案1】:

Y6 似乎是您以不正确的方式调用的元组。由于您没有提供数据,因此很难准确指出哪里出了问题,但以下示例向您展示了如何从 tuple 调用元素:

y = ((1,2,3,4,5),)
print('This one works: ',y[0][1:])
print(y[1:,0])

,结果是这样的:

This one works:  (2, 3, 4, 5)
Traceback (most recent call last):
  File "E:\armatita\stackoverflow\test.py", line 9, in <module>
    print(y[1:,0])
TypeError: tuple indices must be integers, not tuple

【讨论】:

    猜你喜欢
    • 2017-02-11
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 2016-05-23
    • 2015-07-11
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    相关资源
    最近更新 更多