2013-10-28 88 views
1

我在OS X上並使用Encopy(由Enthought inc。編寫)來編寫我的python程序。我把它從here下面的代碼只生成一個點,然後它終止:Matplotlib交互式環境不起作用

from pylab import * 
import time 
t = linspace(0.0, pi, 100) 
x = cos(t) 
y = sin(t) 

ion() # turn on interactive mode 
figure(0) 
subplot(111, autoscale_on=False, xlim=(-1.2, 1.2), ylim=(-.2, 1.2)) 

point = plot([x[0]], [y[0]], marker='o', mfc='r', ms=3) 

for j in arange(len(t)): 
    # reset x/y-data of point 
    setp(point[0], data=(x[j], y[j])) 
    time.sleep(0.05) 
    draw() # redraw current figure 

ioff() # turn off interactive mode 
show() 

任何想法可能是什麼問題? 下面是我得到的結果的照片。 enter image description here

+2

使用'plt.pause'取代'sleep' – tacaswell

+0

@tcaswell,謝謝。但還是單點! – Cupitor

回答

2

它只繪製一點,因爲你只是告訴它繪製一個點。如果你想劃清界線高達j使用以下命令:

from pylab import * 

t = linspace(0.0, pi, 100) 
x = cos(t) 
y = sin(t) 
figure(0) 
subplot(111, autoscale_on=False, xlim=(-1.2, 1.2), ylim=(-.2, 1.2)) 

point, = plot([x[0]], [y[0]], marker='o', mfc='r', ms=3) 

for j in arange(len(t)): 
    # reset x/y-data of point 
    point.set_data(x[:j], y[:j]) 
    plt.pause(0.05) 
    plt.draw() # redraw current figure 
+0

非常感謝,但你改變了什麼導致了差異?我不太明白! – Cupitor

+0

@Naji我建議你手工瀏覽代碼並查看每個函數的功能。 – tacaswell

+0

我能看到的是你改變了point.set_data參數。那麼根據matplotlib的教程,setp用於設置行屬性,我沒有看到任何錯誤的代碼。另外你的代碼還有一個有趣的地方,你甚至不會改變交互模式。怎麼來的?? – Cupitor