2016-03-07 37 views
0

我正在Python中使用for循环运行50多个迭代的模型,并且想要在25次迭代之后绘制它,并且在50次迭代之后再次绘制它。通过for循环中途绘制东西 - python

下面是我迄今为止使用的代码的摘录(如果这会有帮助,我可以发布整个事情)。

ts = np.array([0]) 
xs = f(ts) 

for i in range(50): 
    tn = ts[i]+0.1 
    xn = f(tn) 
    ts = np.append(ts,tn) 
    xs = np.append(xs,xn) 
    while i == 24: 
     plt.plot(ts,xs) 
     plt.savefig('Weight plotted after 2.5 seconds.png') 
    while i == 49: 
     plt.plot(ts,xs) 
     plt.savefig('Spring plotted after 5 seconds.png') 

我没有得到任何错误,但它只是没有返回任何东西。我对python和编码通常很陌生,所以任何人都可能会有什么输入将会非常感谢!

回答

1

您需要用if语句替换while语句。

while只要满足条件i == 24就会重复缩进代码。一旦你的循环达到i == 24,程序将重复保存你的数字,直到你终止程序,因为iwhile循环内没有改变。

if将执行一次缩进代码,如果条件满足 - 这是你想要的。

+0

啊,这应该是显而易见的...感谢您的帮助,我在那里工作! – Alpacaheaven