There are a lot of questions about matplotlib, pylab, pyplot, ipython, so I'm sorry if you're sick of seeing this asked. I'll try to be as specific as I can, because I've been looking through people's questions and looking at documentation for pyplot and pylab, and I still am not sure what I'm doing wrong. On with the code:

Goal: plot a figure every .5 seconds, and update the figure as soon as the plot command is called.

My attempt at coding this follows (running on ipython -pylab):

```
import time
ion()
x=linspace(-1,1,51)
plot(sin(x))
for i in range(10):
plot([sin(i+j) for j in x])
#see **
print i
time.sleep(1)
print 'Done'
```

It correctly plots each line, but not until it has exited the for loop. I have tried forcing a redraw by putting `draw()`

where `**`

is, but that doesn't seem to work either. Ideally, I'd like to have it simply add each line, instead of doing a full redraw. If redrawing is required however, that's fine.

Additional attempts at solving:

just after `ion()`

, tried adding `hold(True)`

to no avail.

for kicks tried `show()`

for `**`

The closest answer I've found to what I'm trying to do was at plotting lines without blocking execution, but `show()`

isn't doing anything.

I apologize if this is a straightforward request, and I'm looking past something so obvious. For what it's worth, this came up while I was trying to convert matlab code from class to some python for my own use. The original matlab (initializations removed) which I have been trying to convert follows:

```
for i=1:time
plot(u)
hold on
pause(.01)
for j=2:n-1
v(j)=u(j)-2*u(j-1)
end
v(1)= pi
u=v
end
```

Any help, even if it's just "look up this_method" would be excellent, so I can at least narrow my efforts to figuring out how to use that method. If there's any more information that would be useful, let me know.

The second answer to the question you linked provides the answer: call `draw()`

after every `plot()`

to make it appear immediately; for example:

```
import time
ion()
x = linspace(-1,1,51)
plot(sin(x))
for i in range(10):
plot([sin(i+j) for j in x])
# make it appear immediately
draw()
time.sleep(1)
```

If that doesn't work... try what they do on this page: http://www.scipy.org/Cookbook/Matplotlib/Animations

```
import time
ion()
tstart = time.time() # for profiling
x = arange(0,2*pi,0.01) # x-array
line, = plot(x,sin(x))
for i in arange(1,200):
line.set_ydata(sin(x+i/10.0)) # update the data
draw() # redraw the canvas
print 'FPS:' , 200/(time.time()-tstart)
```

The page mentions that the `line.set_ydata()`

function is the key part.

```
from pylab import *
import time
ion()
tstart = time.time() # for profiling
x = arange(0,2*pi,0.01) # x-array
line, = plot(x,sin(x))
for i in arange(1,200):
line.set_ydata(sin(x+i/10.0)) # update the data
draw() # redraw the canvas
pause(0.01)
print 'FPS:' , 200/(time.time()-tstart)
ioff()
show()
```

#
The above worked for me nicely. I ran it in spyder editor in pythonxy2.7.3 under win7 OS. Note the pause() statement following draw() followed by ioff() and show().

Licensed under: CC-BY-SA with attribution

Not affiliated with: Stack Overflow