Komplet prekresleni (''cla(); plot(...)''):
#!/usr/bin/python
from pylab import *
import time
figure()
show(False)
while True:
y = np.random.normal(size=10000)
t = time.time()
cla()
plot(y)
draw()
print time.time()-t
Modifikace existujuceho objektu (''line.set_?data(...)''):
#!/usr/bin/python
from pylab import *
import time
figure()
show(False)
y = np.random.normal(size=10000)
l, = plot(y)
while True:
y = np.random.normal(size=10000)
t = time.time()
l.set_ydata(y)
draw()
print time.time()-t
Nahodna prochazka:
#!/usr/bin/python
from pylab import *
t = 2*pi*rand(1000000)
xs = cumsum(sin(t))
ys = cumsum(cos(t))
plot(xs,ys)
show()
#!/usr/bin/python
from pylab import *
x,y = 0,0
xs = [x]
ys = [y]
figure()
show(False)
for i in xrange(500):
t = 2*pi*rand()
x += sin(t)
y += cos(t)
xs.append(x)
ys.append(y)
cla()
plot(xs,ys)
draw()
#!/usr/bin/python
from pylab import *
x,y = 0,0
xs = [x]
ys = [y]
fnames = []
import time
tstart = time.time()
figure()
show(False)
for i in xrange(50):
fname = "walk{:03d}.png".format(i)
print fname
t = 2*pi*rand()
x += sin(t)
y += cos(t)
xs.append(x)
ys.append(y)
cla()
plot(xs,ys)
draw()
savefig(fname)
fnames.append(fname)
print time.time()-tstart
import subprocess, os
print "merging animation"
subprocess.check_call(["convert"] + fnames + ["walk.gif"])
print "deleting temp. files"
for fname in fnames:
os.remove(fname)
{{:python:walk.gif|}}
#!/usr/bin/python
from pylab import *
x,y = 0,0
xs = [x]
ys = [y]
lines = []
import time
tstart = time.time()
f = figure()
for i in xrange(50):
print i
t = 2*pi*rand()
x += sin(t)
y += cos(t)
xs.append(x)
ys.append(y)
lines.append(plot(xs,ys,"b-"))
print time.time() - tstart
from matplotlib.animation import ArtistAnimation
a = ArtistAnimation(f, lines, interval=50, blit=True)
a.save('walker.mp4', 'avconv', fps=20, extra_args=['-vcodec', 'libx264']))
show()
{{:python:walker.mp4|}}
#!/usr/bin/python
from pylab import *
N = 50
t = 2*pi*rand(N)
xs = cumsum(sin(t))
ys = cumsum(cos(t))
f = figure()
l, = plot([],[])
xlim(-10,10)
ylim(-10,10)
def func(i):
print i
l.set_data(xs[:i],ys[:i])
return l,
from matplotlib.animation import FuncAnimation
a = FuncAnimation(f, func, frames=N, interval=50, blit=True)
a.save('walker.mp4', 'avconv', fps=20, extra_args=['-vcodec', 'libx264'])
show()