Python 重构代码以使用3个值进行绘图&;元组索引必须是整数,而不是浮点

Python 重构代码以使用3个值进行绘图&;元组索引必须是整数,而不是浮点,python,matplotlib,Python,Matplotlib,我有一个代码,其中对于3个不同的D值,我有3个不同的dx值,所以,3个不同的图 我想做一个情节,将所有3个情节在一个 ... D=(0.133e-4,0.243e-4,0.283e-4) dx=sc.zeros(3) for i in D: dx[i]=sc.sqrt(D[i]*dt/M) plt.ion() while n<N: Vw_n=Vw_n1 C_n=C_n1

我有一个代码,其中对于3个不同的D值,我有3个不同的dx值,所以,3个不同的图

我想做一个情节,将所有3个情节在一个

...
D=(0.133e-4,0.243e-4,0.283e-4)

dx=sc.zeros(3)
    for i in D:
        dx[i]=sc.sqrt(D[i]*dt/M)

        plt.ion()
        while n<N:
            Vw_n=Vw_n1
            C_n=C_n1
            R2=(Vw_n+B1)/(Vw_0+B1)
            Cc=C_n1[0]/C0
            F2_1=10000/3*Pw*A*(C0*Vw_0/Vw_n1-C_n[1])
            dV=F2_1*dt
            Vw_n1=Vw_n+dV
            C_n1[0]=C0*Vw_0/Vw_n1
            F_i_2=-D[i]/dx[i]*(C_n[1:7]-C_n[0:6])
            C_n1[0:6]=C_n[0:6]-F_i_2*A*dt/(L/(V0/A)*V0/5)
            n+=1
            ttime=n*0.02*1000

            #-----PLOT AREA---------------------------------#

            mylabels=('T=273','T=293','T=298')
            colors=('-b','or','+k')

            if x==1:
                plt.plot(ttime,R2,mylabels[i],colors[i])
            elif x==2:
                plt.plot(ttime,Cc,mylabels[i],colors[i])
            plt.draw()
            plt.show()
。。。
D=(0.133e-4,0.243e-4,0.283e-4)
dx=sc.0(3)
对于D中的i:
dx[i]=sc.sqrt(D[i]*dt/M)
plt.ion()
当n时,考虑以下几行:

D=(0.133e-4,0.243e-4,0.283e-4)
for i in D:
    dx[i]=sc.sqrt(D[i]*dt/M)
i
是一个浮点数。它不能用作元组
D
的索引。 (
D[i]
没有意义。)

也许你的意思是

D=(0.133e-4,0.243e-4,0.283e-4)
for i, dval in enumerate(D):
    dx[i] = sc.sqrt(dval*dt/M)
或者,简单地说

import scipy as sc
D = sc.array([0.133e-4,0.243e-4,0.283e-4])
dx = sc.sqrt(D*dt/M)

  • 不要为每个点调用
    plt.plot
    一次。那条路通向 懒散的行为。相反,累积整个曲线的 数据点,然后调用整个曲线的
    plt.plot
    一次
  • 要在同一图形上绘制3条曲线,只需调用
    plt.plot
    3次。 在调用
    plt.show()
    之前先执行此操作
  • 当您为
    x
    输入
    1
    时,
    while not标志
    循环没有结束, 因为
    如果x==2
    应该是
    elif x==2
  • 若要设置matplotlib绘图的动画,仍应尝试避免多个 调用
    plt.plot
    。相反,使用
    plt.plot
    一次来制作
    Line2D
    对象,然后使用对的调用更新基础数据
    line.set_xdata
    line.set_ydata
    。请参见和中的示例


:(我更新了)我使用scipy数组。现在(奇怪的是为什么?)!您能否提供一个简短、简化、可运行的示例?我不知道这些变量中的任何一个是什么意思,也不知道发生了什么错误。注意:好的,我给了你一个可运行的代码。它适用于一个D值。我想这样做,可以运行3个值。(另外,在一个图中绘制所有3个不同的3个值的图)。如果你看到我以前的代码,你就会明白我在尝试做什么。谢谢!:首先,非常感谢!!几乎完美!有一件事。将plot称为“plt.plot(tvals,yvals,“-”)会导致混合绘图。我尝试了类似于:colors=('ob','-r','+m'),但我不知道如何将其放入上面的plot调用中。有什么方法吗?为了符合matplotlib术语,我将
colors
-->
线型
。您可以使用
来循环查看
D
linestyles
的值,对于dval,zip中的linestyle(D,linestyles)
。上面的代码已经被编辑以显示我的意思。
import scipy as sc
D = sc.array([0.133e-4,0.243e-4,0.283e-4])
dx = sc.sqrt(D*dt/M)
import scipy as sc
import matplotlib.pyplot as plt

def graph(x):
    plt.ion()
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    lines = []
    D = (0.133e-4, 0.243e-4, 0.283e-4)
    temperatures = ('T = 273','T = 293','T = 298')
    N = 2000
    linestyles = ('ob', '-r', '+m')
    for dval, linestyle, temp in zip(D, linestyles, temperatures):
        line, = ax.plot([], [], linestyle, label = temp) 
        lines.append(line)
    plt.xlim((0, N*0.02*1000))        
    if x == 1:
        plt.ylim((0.7, 1.0))
    else:
        plt.ylim((1.0, 1.6))
    plt.legend(loc = 'best')        
    for dval, line in zip(D, lines):
        A = 1.67e-6
        V0 = 88e-12
        Vw_n1 = 71.7/100*V0
        Pw = 0.22
        L = 4e-4
        B1 = V0-Vw_n1
        C7 = 0.447e-3
        dt = 0.2e-4
        M = 0.759e-1
        C_n1 = sc.zeros(7)
        C_n1[0:6] = 0.290e-3
        C_n1[6] = 0.447e-3
        C0 = C_n1[0]
        Vw_0 = Vw_n1

        tvals = []
        yvals = []
        dx = sc.sqrt(dval*dt/M)
        for n in range(1, N+1, 1):
            Vw_n = Vw_n1
            C_n = C_n1
            R2 = (Vw_n+B1)/(Vw_0+B1)
            Cc = C_n1[0]/C0
            F2_1 = 10000/3*Pw*A*(C0*Vw_0/Vw_n1-C_n[1])
            dV = F2_1*dt
            Vw_n1 = Vw_n+dV
            C_n1[0] = C0*Vw_0/Vw_n1
            F_i_2 = -dval/dx*(C_n[1:7]-C_n[0:6])
            C_n1[0:6] = C_n[0:6]-F_i_2*A*dt/(L/(V0/A)*V0/5)
            tvals.append(n*0.02*1000)
            yvals.append(R2 if x == 1 else Cc)
            if not len(yvals) % 50:
                line.set_xdata(tvals)
                line.set_ydata(yvals)
                fig.canvas.draw()

if __name__ == "__main__":
    flag = False
    while not flag:
        try:
            x = int(raw_input("Give a choice 1  or 2  : "))
            flag = True
            if x == 1:
                plt.title('Change in cell volume ratio as a function of time \n\
                at various temperatures')
                plt.xlabel('Time')
                plt.ylabel('Ceil volume ratio (V/V0)')
                graph(x)
            elif x == 2:
                plt.title('Increase of solute concentration at various temperatures')
                plt.xlabel('Time')
                plt.ylabel('Solute concentration in the Ceil (Cc)')
                graph(x)
            else:
                flag = False
                print("You must input 1 or 2")
        except ValueError:
            print("You must input 1 or 2")
    raw_input('Press a key when done')