我有一个代码,其中对于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()
---------- RUNNABLE --------------------------
import scipy as sc
import matplotlib.pyplot as plt
def graph(x):
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
N=2000
n =1
D = ,0.243e-4
dx = sc.sqrt(D*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/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)
n+=1
ttime=n*0.02*1000
#-----PLOT AREA---------------------------------#
if x==1:
plt.plot(ttime,R2)
elif x==2:
plt.plot(ttime,Cc)
plt.draw()
plt.show()
我的问题是我想绘制(ttime,R2)和(ttime,Cc)。 但我无法想象如何调用R2和Cc来获得D(和dx)的3个不同值。
另外,我发错了:元组索引必须是整数,而不是浮点数
at dx [i] = sc.sqrt(D [i] * dt / M)。
谢谢!
答案 0 :(得分:2)
考虑以下几点:
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
一次。plt.plot
3次即可。
在致电plt.show()
之前先执行此操作。while not flag
输入1
时,x
循环未结束,
因为if x==2
应该是elif x==2
。plt.plot
。相反,请使用plt.plot
一次制作Line2D
对象,然后通过调用更新基础数据
line.set_xdata
和line.set_ydata
。请参阅Joe Kington's example中的matplotlib docs和此示例。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')