简体   繁体   中英

Matplotlib interactive environment does not work

I am on OS X and using Encopy(by Enthought inc.) to write my python programs. The following code which I took it from here only generates one point and then it terminates:

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()

Any Ideas what might be the problem? And below is the photo of the result I get. 在此处输入图片说明

It is only plotting one point because you are only telling it to plot one point. If you want to draw the line up to j use the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM