User Tools

Site Tools


Sidebar

Predmety

Zariadenia

Users

python:animation

Komplet prekresleni (cla(); plot(…)):

a1.py
#!/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(…)):

a1.py
#!/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:

walk0.py
#!/usr/bin/python
from pylab import *
 
t = 2*pi*rand(1000000)
xs = cumsum(sin(t))
ys = cumsum(cos(t))
plot(xs,ys)
show()
walk1.py
#!/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()
walk2.py
#!/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)

walk3.py
#!/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()

walk4.py
#!/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()
python/animation.txt · Last modified: 2021/10/25 08:53 (external edit)