2015-01-08 90 views
2

當您運行此操作時,它會以20爲單位創建一個從100到200的表格,並計算所需的時間。我想要一個顯示這兩者之間關係的圖,但是當我運行它時,圖是空白的,而x和y圖的數字是不正確的。 (即,在y軸上,我想速度(100 - 200)爲什麼我的圖表空白並且數字不正確

import pylab 

print "Speed (mph) | Time Taken (s)" 
print "===========================" 

for i in range(100,201,20): 
    Speed = i 
    Lowest_Speed = pylab.array([0.0,100.0,30.0],float) 
    Distance_Travelled = pylab.array([20000.0,20000.0,10000.0],float) 
    Average_Speed = (Speed + Lowest_Speed)/2.0 
    t= Distance_Travelled/Average_Speed 
    time_taken=t[0]+t[1]+t[2] 


print "%5d"%Speed,"%12.3f"%time_taken 

pylab.title ("Speed vs Time Taken") 
pylab.xlabel ("Time Taken (s)") 
pylab.ylabel ("Speed (mph)") 
pylab.plot ([time_taken] ,[Speed] , 'r-') 
pylab.show() 

我想200下降到100 20的步驟,並且我想420將753(ASK示於計算)因此。呈負相關

回答

2

好像你只密謀已分配給Speedtime_taken在循環的最後價值觀相反,你可能要所有這些值添加到列表中,並繪製這些名單:

... 
all_speeds = [] 
all_times = [] 
for i in range(100, 201, 20): 
    ... 
    all_speeds.append(Speed) 
    all_times.append(time_taken) 

... 
pylab.plot(all_times, all_speeds, 'r-') 
pylab.show() 
+1

另外,'print'%5d「%Speed」,%12.3f「%time_taken'命令應該縮進進入循環以打印所有迭代 – Schorsch

相關問題